Azzera filtri
Azzera filtri

Use num2str in for loop

5 visualizzazioni (ultimi 30 giorni)
Mac
Mac il 29 Ott 2023
Commentato: Mac il 29 Ott 2023
Good day. I know is simple but I can't find the solution to this. I want to call some data (M3 to M11) in for loop using num2str, but it does not work. Below is the code, specifically in bold.
M3 = mean(Mar, 3);
M4 = mean(Apr, 3);
M5 = mean(May, 3);
M6 = mean(Jun, 3);
M7 = mean(Jul, 3);
M8 = mean(Aug, 3);
M9 = mean(Sep, 3);
M10 = mean(Oct, 3);
M11 = mean(Nov, 3);
[xx,yy]=meshgrid(lon,lat);
b=-18:2:18;
f = figure;
t = tiledlayout(f, 3, 3);
for i = 3:11
nexttile(t)
m_proj('miller','lon',[110 130],'lat',[0 15]);
m_contourf(xx,yy,['M', num2str(i)], b,'linestyle','none');
colormap(m_colmap('BOD',256))
................
  12 Commenti
Stephen23
Stephen23 il 29 Ott 2023
Mac
Mac il 29 Ott 2023
Thanks @Stephen23. Appreciate it.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 29 Ott 2023
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.
M3 = mean(Mar, 3);
M4 = mean(Apr, 3);
M5 = mean(May, 3);
M6 = mean(Jun, 3);
M7 = mean(Jul, 3);
M8 = mean(Aug, 3);
M9 = mean(Sep, 3);
M10 = mean(Oct, 3);
M11 = mean(Nov, 3);
M = {[], [], M3, M4, M5, M6, M7, M8, M9, M10, M11}; %two dummy so M{i} corresponds to M_i
for i = 3:11
nexttile(t)
m_proj('miller','lon',[110 130],'lat',[0 15]);
m_contourf(xx,yy,M{i}, b,'linestyle','none');
colormap(m_colmap('BOD',256))
  1 Commento
Mac
Mac il 29 Ott 2023
Thanks a lot Walter. Noted. Thank to both for the enlightenment.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by