0

I have many .txt files like these: "u1.txt", "i3.txt", "p10.txt"... How to load all of these files to matlab, to variables like these: "u1", "u2"... "p1"... Here is my code:

clc, clear all
%% loading data
for j=0:3
    switch j
        case 0
            variable='i';
        case 1
            variable='u';
        case 2
            variable='p';
        case 3
            variable='q';
    end
    for i=0:15
        name = strcat(variable, int2str(i), '.txt')
        fid=fopen(name,'r');
        data=textscan(fid,'%*s%*s%s%s%s%*s','HeaderLines',10,'CollectOutput',1);
        fclose(fid);
        data=strrep(data{1},',','.');
        data=cellfun(@str2num, data);
    end
end

Problem is with variable data - how to change this variable to: "u1", "u2"... "p1"... after every loop?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kulis
  • 988
  • 3
  • 11
  • 25
  • 3
    I would avoid using variable names `i` and `j`: http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab – Daniel Aug 22 '14 at 12:20
  • 3
    You might also use the `dir` function so that you don't have to know the variable names ahead of time if you don't want. – David K Aug 22 '14 at 12:26

3 Answers3

2

You could use the variable names u1 u2 etc.. but I would strongly recommend not to do so. Then these consecutive variables u1 to u15 are totally individual variables and basically matlab can not iterate over these variables. For this purpose, I would use a struct which contains cell arrays. Use this line to assign:

allData.(variable){i}=data

And to get your data, instead of u1 use allData.u{1}. These are some more characters to write, but having such structured data results in much simpler code when you use the data.

//Code:

for j=0:3
    switch j
        case 0
            variable='i';
        case 1
            variable='u';
        case 2
            variable='p';
        case 3
            variable='q';
    end
    for i=0:15
        name = strcat(variable, int2str(i), '.txt')
        fid=fopen(name,'r');
        data=textscan(fid,'%*s%*s%s%s%s%*s','HeaderLines',10,'CollectOutput',1);
        fclose(fid);
        data=strrep(data{1},',','.');
        data=cellfun(@str2num, data);
        allData.(variable){i}=data;
    end
end
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • To do this I have to change these lines: `data=strrep(data{1},',','.'); data=cellfun(@str2num, data);` Any suggestion? – Kulis Aug 22 '14 at 13:11
  • 1
    @Kulis: Insert after `data=cellfun(@str2num, data);`, I assume then you have stored in `data` what you want to keep. – Daniel Aug 22 '14 at 13:20
  • 1
    @Kulis: I don't understand what your changed is supposed to do, it simply does not contain the line I suggested to use. My Answer now contains complete code. – Daniel Aug 22 '14 at 13:59
1

Use eval.

For example:

x = input('Enter the name of the new variable: ','s');
eval([x,'=0:4;']);

In your case:

variablename = strcat(variable,int2str(i));
eval([variablename,'=cellfun(@str2num,',variablename,')'];

This is a good read: Creating variables on the run

lakshmen
  • 28,346
  • 66
  • 178
  • 276
1

You could use eval to do this:

eval([name '=textscan(fid,'%*s%*s%s%s%s%*s','HeaderLines',10,'CollectOutput',1);']);

I think that syntax is correct but I'm not sure. You might have to play around with it a bit to get it to work, which is a huge drawback to using eval in the first place. Not to mention the syntax parser can't work with it, making it more difficult to debug.

My recommendation would be to utilize MATLAB's capability to use dynamic fieldnames.

data.(name)=textscan(fid,'%*s%*s%s%s%s%*s','HeaderLines',10,'CollectOutput',1);

Much cleaner and much easier to debug.

My other recommendation is that you evaluate why you need to utilize this naming scheme in the first place. It will be much easier to create an array (numeric or cell) and use that numeric ID as an index rather than include it with the variable name.

sco1
  • 12,154
  • 5
  • 26
  • 48
  • I use these variables in my simulink model, so this is the point why I preffer these names. – Kulis Aug 22 '14 at 12:34