Question

kinda new to matlab here, searching the csvwrite tutorial and some of the existing webportals regarding my question couldn't find a way to pass my variables by value to the output file names while exporting in csv; providing my bellow scripts, i would like to have the output files something like output_$aa_$dd.csv which aa and dd are respectively the first and second for counters of the scripts.

for aa=1:27
for dd=1:5
    M_Normal=bench(aa,dd).Y;
    for j=1:300
        randRand=M_Normal(randperm(12000,12000));
            for jj = 1:numel(randMin(:,1));  % loops over the rand numbers
                vv= randMin(jj,1);  % gets the value
                randMin(jj,j+1)=min(randRand(1:vv));    % get and store the min of the selction in the matix
            end
    end
    csvwrite('/home/amir/amir_matlab/sprintf(''%d%d',aa, bb).csv',randMin);
end
end
Was it helpful?

Solution

String concatenation in MATLAB is done like a matrix concatenation. For example

a='app';
b='le';
c=[a,b] % returns 'apple'

Hence, in your problem, the full path can be formed this way.

['/home/amir/amir_matlab/',sprintf('%d_%d',aa,bb),'.csv']

Furthermore, it is usually best not to specify the file separator explicitly, so that your code can be implemented in other operating systems. I suggest you write the full path as

fullfile('home','amir','amir_matlab',sprintf('%d_%d.csv',aa,bb))

Cheers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top