Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
need help with loops

Subject: need help with loops

From: Kirk

Date: 6 May, 2011 16:10:22

Message: 1 of 1

I have a large number of model iterations saved in a struct format that I need to work on. I am trying to figure out how to use for loops to pull data out and export it in a flat file (2 dimensional matrix or dataset array) format. Currently, the script loads are large struct, and selects a series of rows in for each desired column, creating a new struct with new field names. Then the script calculates means from desired columns and used the cat function to combine those data into a new flat file matrix.

The problem is that if this approach is used to work on more than a few iterations of the data (currently I am working working the first 50 iterations / columns, but I may need to work on more 100 columns or 500 columns or 1000 columns)... As you can see it becomes impractical to write a new line of code for each column of data.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Load climate variables and build struct from years
load ../../_output/ra1000/dl/full/allClimateData.mat

% Year 2001-2200(rows12001:13800) for the first 50 iterations(columns 1:50).
Clim.tmax1 = tmax(11989:13788,1);
Clim.tmax2 = tmax(11989:13788,2);
Clim.tmax3 = tmax(11989:13788,3);
Clim.tmax4 = tmax(11989:13788,4);
Clim.tmax5 = tmax(11989:13788,5);
etc...

% Calculate struct growing season means(months 4:10), and convert 50 element
% structs into a single column with elements stacked in vertical
% order(7500:1).
dlclim49xs_mgstmax = cat(1, ...
    mean(Clim.tmax1(4:10,:),1)', ...
    mean(Clim.tmax2(4:10,:),1)', ...
    mean(Clim.tmax3(4:10,:),1)', ...
    mean(Clim.tmax4(4:10,:),1)', ...
    mean(Clim.tmax5(4:10,:),1)', ...
etc...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

I think I need to be able to loop the workflow where each pass through the loop grabs a new iteration of data. Something like

for n = 1:50
     Clim.tmax[n] = tmax(11989:13788,[n]);

where n is the iteration number.

I am even on the right track here?
     

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us