First calculating then looping with multiple figures

1 view (last 30 days)
Hi all
I am trying to combine some matrices, all with the same dimensions but different names. More specifically the quantities I am trying to assess have the same name of variable but a different timestep indicating their recorded time (e.g. Windv_y_20040501_180000)
I usually have used, when i first time started to analyse them, which give me a moving figure in matlab
[a1 a2 a3]=size(Windv_y_20040501_180000);
v10_profile=reshape(Windv_y_20040501_180000,a1,a2*a3);
for a=1:a3
imagesc(V10(:,:,a))
title('wind profile m/s V_1_0'),colorbar
drawnow;
pause(0.2);
end
Although this does not satisfy the next thing i want to achieve which is to combine two quantities and then have a figure(subplot) with the new combined re-calculated matrices in a separate plot
The files i normally use have this kind of structure, a constant name of variable but different endings.
Windv_y_20040501_180000
Windv_y_20040501_210000
Windv_x_20040501_180000
Windv_x_20040501_210000
Assuming that i want to automate a process for example, adding the corresponding matrices and then plot them
m1= Windv_x_20040501_180000 + Windv_y_20040501_180000
m2= Windv_x_20040501_210000 + Windv_y_20040501_210000
figure
subplot(2,1,1),image(m1)
subplot(2,1,2),image(m2)
Any suggestion would be welcome
thank you
  4 Comments
George
George on 17 Oct 2014
The number of files varies (from 4to 26, meaning two from each quantity), I would like to avoid merging them into a single variable, because for post processing purposes, I have to keep the m1 , m2 ...mn for future usage.
As a first step i want to somehow iterate the appropriate varnames into a formulation I have constructed to get m1 (the addition here is just an example).
Jon Boerner
Jon Boerner on 20 Oct 2014
I am still not quite sure what your whole workflow is, but going on what George suggested, you could keep the variables in a larger structure so that their names are maintained. For example:
mydata.Windv_x_20040501_180000 = Windv_x_20040501_180000;
mydata.Windv_y_20040501_180000 = Windv_y_20040501_180000;
Then you could iterate over the variables using the fieldnames function:
>> fields = fieldnames(mydata)
ans =
'Windv_x_20040501_180000'
'Windv_y_20040501_180000'
And then using a loop:
for i=1:length(fields)
m = mydata.(fields{i});
% Plotting code...
end
You could do some more complicated logic to pull out fields with the same timestep and add them together or something like that using the field names as well.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 17 Oct 2014
You could create a cell array of n elements and so still keep the data sets distinct. If you are reading from n files then it would be something like
windVData = cell(n,2);
for k=1:n
% read x and y data from file
% some code here
% save to cell array
windVData(k,1) = xData;
windVData(k,2) = yData;
end
You could even save the file name in a third column of the array (if you wish).
Then you could iterate over this cell array and produce your subplots
figure;
n = size(windVData,1);
for k=1:n
% create a single column of n subplots
h = subplot(n,1,k);
% add the two vectors (assumed to be of the same dimension)
m1= windVData{k,1} + windVData{k,2};
% display the data
image(m1,'Parent',h);
end
Not really clear if you wish to use image or plot instead.
  3 Comments
Geoff Hayes
Geoff Hayes on 17 Oct 2014
Right - the two columns in the cell array correspond to the x and y data for a specific time step. The above just gives a general idea of what you can do (since you haven't shown how you load/read the different variables or time steps).
Though I'm not sure what you are hinting at with your assignment of i, as 20041231_180000 cannot be used as an index into m or suffixed onto Windv_y_.
As for plotting, is your data the wind velocity in the x direction and wind velocity in the y direction? What does the 300x300 mean? What does image show you when you add the two together? Since you haven't described what this data represents, I can't provide any (good) suggestions on how it should be displayed.
George
George on 20 Oct 2014
By combining the components I get the overall wind resource, each component basically is 300x300 matrix (or more) and represents a gridded mesh, with the each point of the having a assigned U, or V value, thus the existence of the tableS. Image basically is used, either that or contour to show the mesh itself

Sign in to comment.

Categories

Find more on Line Plots 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!