Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Why is it that textread works fine with txt files and not dat files in this example?

Subject: Why is it that textread works fine with txt files and not dat files in this example?

From: Diego Zegarra

Date: 5 Feb, 2009 06:04:02

Message: 1 of 3

I have a problem using the textread function. It works when I use .txt files but not when I use .dat files.

The .dat file looks like this,
%--------------------------------------------
2
4

51 59
99 92
61 56
83 82

84 83 77 56
92 97 85 62
66 91 98 56
66 57 72 97

82 79 71 84
80 86 91 78
81 51 83 52
88 91 54 86

%---------------------------------------------

The following function is used,

%---------------------------------------------
function [numMachines, numJobs, PT, ST] = fgetmat1(runfile)

    % Get matrices A, PT and ST{i} from text file
    % Convert file to a matrix
    data = textread(runfile);
    
    % Get the # of machines and jobs from file
    numMachines = data(1,1);
    numJobs = data(2,1);

    % Save Machines, Jobs to a matrix
    nextRowIndex = 2;

    % Saves the processing times to a matrix
    CPT = nextRowIndex+(1:numJobs);
    PT = data(CPT, 1:numMachines);

    % Saves the setup times to an array
    ST = cell(numMachines,1);
    for idx = 1:numMachines
        nextRowIndex = nextRowIndex + numJobs;
        CPT = nextRowIndex+(1:numJobs);
        ST{idx} = data(CPT, 1:numJobs);
    end
%---------------------------------------------

And I call it the following way,

% --- Select the directory to run the files
files = dir('F:\TESIS\Code\*.txt'); %---- Works with .txt but not .dat file! Why?

for FNum = 1:length(files);
    Fname = files(FNum).name;

    % Get the text file given and extract data from it
    [numMachines, numJobs, PT, ST] = fgetmat1(Fname);
end

I get the following error message,

??? Error using ==> textread
File not found.

Error in ==> fgetmat1 at 5
    data = textread(runfile);

Error in ==> DV3SAPSLMETARAPS at 21
    [numMachines, numJobs, PT, ST] = fgetmat1(Fname);

And if you have any doubt I have debugged the code and Fname DOES contain a file, it is not empty.
 
Thanks for any help provided! If anyone can point me in the right direction or if there is something wrong with my code... it would be greatly appreciated! If there is any unclear information let me know so I can specify better.

Subject: Why is it that textread works fine with txt files and not dat files in this example?

From: Pekka Kumpulainen

Date: 5 Feb, 2009 07:07:02

Message: 2 of 3

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gmdvgi$co1$1@fred.mathworks.com>...
> I have a problem using the textread function. It works when I use .txt files but not when I use .dat files.
>
> The .dat file looks like this,
> %--------------------------------------------
> 2
> 4
>
> 51 59
> 99 92
> 61 56
> 83 82
>
> 84 83 77 56
> 92 97 85 62
> 66 91 98 56
> 66 57 72 97
>
> 82 79 71 84
> 80 86 91 78
> 81 51 83 52
> 88 91 54 86
>
> %---------------------------------------------
>
> The following function is used,
>
> %---------------------------------------------
> function [numMachines, numJobs, PT, ST] = fgetmat1(runfile)
>
> % Get matrices A, PT and ST{i} from text file
> % Convert file to a matrix
> data = textread(runfile);
>
> % Get the # of machines and jobs from file
> numMachines = data(1,1);
> numJobs = data(2,1);
>
> % Save Machines, Jobs to a matrix
> nextRowIndex = 2;
>
> % Saves the processing times to a matrix
> CPT = nextRowIndex+(1:numJobs);
> PT = data(CPT, 1:numMachines);
>
> % Saves the setup times to an array
> ST = cell(numMachines,1);
> for idx = 1:numMachines
> nextRowIndex = nextRowIndex + numJobs;
> CPT = nextRowIndex+(1:numJobs);
> ST{idx} = data(CPT, 1:numJobs);
> end
> %---------------------------------------------
>
> And I call it the following way,
>
> % --- Select the directory to run the files
> files = dir('F:\TESIS\Code\*.txt'); %---- Works with .txt but not .dat file! Why?
>
> for FNum = 1:length(files);
> Fname = files(FNum).name;
>
> % Get the text file given and extract data from it
> [numMachines, numJobs, PT, ST] = fgetmat1(Fname);
> end
>
> I get the following error message,
>
> ??? Error using ==> textread
> File not found.
>
> Error in ==> fgetmat1 at 5
> data = textread(runfile);
>
> Error in ==> DV3SAPSLMETARAPS at 21
> [numMachines, numJobs, PT, ST] = fgetmat1(Fname);
>
> And if you have any doubt I have debugged the code and Fname DOES contain a file, it is not empty.
>
> Thanks for any help provided! If anyone can point me in the right direction or if there is something wrong with my code... it would be greatly appreciated! If there is any unclear information let me know so I can specify better.

Do you mean that if you rename the *.txt files to *.dat thing stop working?
Looks like you pass to fgetmat1 only the name of the file without path.
What is your working folder? If it is not F:\TESIS\Code\ the files will not be found without full path included. Do you have the .txt files also in your current folder and therefore they seem to work??

Subject: Why is it that textread works fine with txt files and not dat files in this example?

From: Diego Zegarra

Date: 5 Feb, 2009 16:28:02

Message: 3 of 3

Thanks a lot! That was the problem, they were in different directories. I fixed it already by assigning that path to be used by MatLab as well.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us