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:
function text read

Subject: function text read

From: Diego Zegarra

Date: 4 Feb, 2009 21:27:01

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);


Thanks for any help provided!

Subject: function text read

From: someone

Date: 4 Feb, 2009 21:51:01

Message: 2 of 3

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gmd175$jbu$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);
>
>
> Thanks for any help provided!

% I assume you changed:
files = dir('F:\TESIS\Code\*.txt'); %---- Works with .txt but not .dat file! Why?
% to:
files = dir('F:\TESIS\Code\*.dat'); %---- Works with .txt but not .dat file! Why?

% Remove the semicolon from the line:
Fname = files(FNum).name
% and see what the value of Fname is. Is it empty, maybe?

Subject: function text read

From: Diego Zegarra

Date: 4 Feb, 2009 23:11:02

Message: 3 of 3

It has the following filename,

Fname = 100on2Rp50Rs50_1.dat

It works with a .txt file but not a .dat file.

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