How to create a matrix with several days using eval fuction?

2 views (last 30 days)
First of all, sorry for my english.
for i=1:2
dia={eval(['day' num2str(i) '= i'])}
figure
pcolor(longitude(lon_useful),latitude(lat_useful),squeeze(temperatura(lon_useful,lat_useful,i))'),
title (day{i})
shading flat
colorbar
daspect([1,1,1])
end
I have this code and everytime he runs I want to create a plot with the title "day 1" for the first time, "day 2" for the second and i want to creat a matrix that have "day i" for 'i' times in order to get easier and to change day={'day1','day2'} .
I hope you guys could help me.

Accepted Answer

Steven Lord
Steven Lord on 31 Oct 2020
There's no need to use eval here, and in general if you feel you need to use eval that's often a code smell.
To assemble text using fixed strings and numbers there are several possibilities. In my order of preference:
  • Create a string array.
n = 5;
d = "day " + n
You can even make several strings at once:
m = 1:5;
d2 = "day " + m
d2(3) % this is "day 3"
  • Use num2str or sprintf.
d = ['day ' num2str(5)]
d2 = sprintf('day %d', 5)
  2 Comments
Tiago Martinho
Tiago Martinho on 23 Nov 2020
Can you help me with something please??
Inside of this cicle for, but with the i=1:15, to create 15 figures of 15 differents days, I want to create a matrix of a profile, with the depth and the temperature for 5 different values of depth. How can I do that inside of this cicle? I tried to use other cicle for, but keeps giving me an error.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!