Filter löschen
Filter löschen

How to generate different filename with each iteration of code?

2 Ansichten (letzte 30 Tage)
Devendra
Devendra am 7 Mär. 2024
Bearbeitet: Voss am 8 Mär. 2024
I am using the following synetex of matlab code
saveas(f, path + var + method + '.png');
where var and method are changing in each iteration. I mean there are two methods and two vars as follows
I wish to generate four png files with the combination of methods and vars using following code.
path = 'C:\chahar\';
methods = {'OLS', 'RF'};
dependent_vars = {'act_y_ha', 'act_per_ha'};
for m = 1:length(methods)
method = methods(m);
for jj = 1:numel(dependent_vars)
var = dependent_vars{jj};
% Save the figure
saveas(f, path + method + var + '.png');
end
end
Please suggest me how to fix the error as follows
Arrays have incompatible sizes for this operation.
saveas(f, path + var + method + '.png');
Related documentation

Antworten (1)

Voss
Voss am 7 Mär. 2024
Bearbeitet: Voss am 7 Mär. 2024
mypath = "C:\"; % string (with double quotes)
methods = {'OLS', 'RF'};
dependent_vars = {'act_y_ha', 'act_per_ha'};
% string concatenation
filenames = mypath + dependent_vars.' + methods + '.png'
filenames = 2×2 string array
"C:\act_y_haOLS.png" "C:\act_y_haRF.png" "C:\act_per_haOLS.png" "C:\act_per_haRF.png"
  3 Kommentare
Voss
Voss am 7 Mär. 2024
Bearbeitet: Voss am 7 Mär. 2024
mypath = "C:\chahar\"; % string (with double quotes)
methods = {'OLS', 'RF'};
dependent_vars = {'act_y_ha', 'act_per_ha'};
% string concatenation
filenames = mypath + dependent_vars.' + methods + '.png';
for m = 1:length(methods)
for jj = 1:numel(dependent_vars)
% Save the figure
saveas(f, filenames(jj,m));
end
end
Voss
Voss am 8 Mär. 2024
Bearbeitet: Voss am 8 Mär. 2024
Or, if you prefer:
mypath = 'C:\chahar\'; % character vector (with single quotes)
methods = {'OLS', 'RF'};
dependent_vars = {'act_y_ha', 'act_per_ha'};
for m = 1:length(methods)
method = methods{m};
for jj = 1:numel(dependent_vars)
var = dependent_vars{jj};
% Save the figure
saveas(f, [mypath,method,var,'.png']); % character vector concatenation
end
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Report Generator finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by