1

As an intermediate step to analysis I need to populate a variable called 'files' with the index of current iteration. So far I am doing it manually. I wouldn't like to do it manually for i=1:1000 so I am looking for some automation here, I experimented with many functions but nothing is working. Please help.

Thanks.

Given below is the code snipped I am running.

clear; clc;
    for i=1:10
    files{i}.data = {
        {
        ['1.csv']
        ['2.csv']
        ['3.csv']
        ['4.csv']
        ['5.csv']
        ['6.csv']
        ['7.csv']
        ['8.csv']
        ['9.csv']
        ['10.csv']
        }};
    end

Required structure of file variable Required structure of data variable


Update:

I am using this script

clear; clc;
wdir = 'path\';
all_files = 10;
for i=1:10
files{i}.data = {
    {
        cellstr(strcat(wdir,num2str((1:all_files).'),'.csv'))
    }};
end

Everything is good about this script except the string concat function, It generates space between path and file number for 1..9 files and file no. 10 is perfect. Please help me to fix this. I am getting something like this. WHy this whitespace

3 Answers3

3

You may combine compose and cellstr to obtain the desired cell array:

>> wdir = 'path\';
>> X = 1:10;
>> formatSpec = "%s%d.csv";
>> cellstr(compose(formatSpec,wdir,X)).'

ans =

  10×1 cell array

    {'path\1.csv' }
    {'path\2.csv' }
    {'path\3.csv' }
    {'path\4.csv' }
    {'path\5.csv' }
    {'path\6.csv' }
    {'path\7.csv' }
    {'path\8.csv' }
    {'path\9.csv' }
    {'path\10.csv'}

For releases of MATLAB prior to R2016b you may use a loop and sprintf:

>> wdir = 'path\';
>> X = 1:10;
>> formatSpec = '%s%d.csv';
>> arrayfun(@(x) sprintf(formatSpec,wdir,x),X,'un',0).'

ans =

  10×1 cell array

    {'path\1.csv' }
    {'path\2.csv' }
    {'path\3.csv' }
    {'path\4.csv' }
    {'path\5.csv' }
    {'path\6.csv' }
    {'path\7.csv' }
    {'path\8.csv' }
    {'path\9.csv' }
    {'path\10.csv'}
Paolo
  • 21,270
  • 6
  • 38
  • 69
0

If I understand the question correctly it would be something like this:

files_length = 1000;
for id=1:files_length
    files{id}.data = {sprintf('%d.csv', id)};
end
mpaskov
  • 1,234
  • 2
  • 11
  • 21
  • I am sorry, probably I wasn't clear but this only generates 1 variable, what I want is 1:10 records in files{i}.data variable –  Sep 20 '18 at 21:02
  • @UnbearableLightness Sorry I unintentionally wronged you. I wanted to share the right answer with the community. I am extremely sorry. Please put your answer It was you who deserve the credit. I wasn't expecting that you will post answer. –  Sep 20 '18 at 21:21
  • @Đēēpak Yeah I misunderstood the OP. – mpaskov Sep 21 '18 at 13:33
-1

Long in short, you could try command 'eval' in Matlab. Idk if you are familiar with python, basically 'eval' in Matlab does the same as what 'exec' does in python.

A very simple example below:

for i=1:10
   eval(['f',num2str(i),'=[];'])
end

you'll get f1, f2, f3...... all the way to f10, while they are all empty matrix.

Lujun.Z
  • 1
  • 2
  • 2
    I don’t think this is what OP is asking. Also, creating variables like this is dangerous: you’ll pretty quickly get hard to find bugs and unmaintainable code. This practice is strongly adviced against. – Cris Luengo Sep 21 '18 at 01:44
  • First of all, thanks for your reply! I wonder could you give me few examples of the "maintenance problem" you mentioned above? Because to me, I feel like 'eval' is really the most convenient tool in Matlab when it comes to dynamic variable name assignment. – Lujun.Z Sep 21 '18 at 01:51
  • 2
    Read for example this answer: https://stackoverflow.com/a/32467170/7328782 – Cris Luengo Sep 21 '18 at 03:38
  • Thank you so much Chris! Thanks for sharing this knowledge! – Lujun.Z Sep 22 '18 at 05:08