loops

If I have a cell structure q1{1,i}{j,1} where i=1:18, and j=1:20 how is it possible to write a loop that can be used to extract data from the final cell (360x35). I can do it for individual cells i.e.
Temp=q1{1,1}{1,1}(:,1:17)...Temp=q1{1,2}{2,1}(:,1:17)...etc.,
i.e. I need all of the rows and columns 1:17 from the dataset. This works fine when running for every individual cell but when trying to write a loop for all of the cells it doesnt seem to work.
Any ideas?
cheers

3 个评论

Jan
Jan 2011-11-16
Please post the code and explain explicitely, what "doesn't seem to work" means.
{1,i}{j,1} is unusual. Would it possible to use {j,i} instead?
ricco
ricco 2011-11-16
Ive managed to make it work. My loop is:
for i=1:length(q2);
for j=1:length(q2{1,1});
T2{1,i}{j,1}=q2{1,i}{j,1}(:,1:17);
end
end
I think I've over complicated it by having so many cells.
ricco
ricco 2011-11-16
Will you be able to look at the following code and tell me if I'm taking a really long winded way of obtaining the result that I get. Ive basically just imported data from excel into matlab and then attempted to create one matrix for some of the data. I have used many loops in attempting to do this and doesn't look like efficient coding. My code is:
%IMPORT THE DATA FROM EXCEL INTO MATLAB-this imports all of the %excel spreadsheets and all of the worksheets in each %spreadsheet.
for a=1:length(files);
File_Name{a}=files(a,1).name;%Removes the file names from %files.
data{a}=importdata(File_Name{a});%import the data using %File_Name
end
%create a cell array where each cell is a seperate spreadsheet %and then each cell within the cell is a seperate worksheet. %Also, remove the notes section which are imported from excel.
for b=1:length(data);
BT_09{b}=struct2cell(data{1,b}.data);%convert the structure %into a cell
BT_09{b}=BT_09{b}(1:length(BT_09{1,b})-1);
end
%each cell has the same number of cells within apart from the %last cell therefore I seperatee the cells and work on them %individually.
BT_09_1=BT_09(1:length(BT_09)-1);
BT_09_2=BT_09(length(BT_09));%last cell which is of a different size
%within the data set, columns 1:17 refer to temperature, %therefore in order to obtain a variable for temperature we %complete the following: Need to do for the seperate cells %individually.
for c=1:length(BT_09_1);
for d=1:length(BT_09_1{1,1});
Temp_1{1,c}{d,1}=BT_09_1{1,c}{d,1}(:,1:17);
end
end
for e=1:length(BT_09_2);
for f=1:length(BT_09_2{1,1});
Temp_2{1,e}{f,1}=BT_09_2{1,e}{f,1}(:,1:17);
end
end
%After specifying which data refers to temperature, I convert %each of the cells within the cells into matrices.
for g=1:length(Temp_1);
Temp_1{g}=cell2mat(Temp_1{1,g});
end
for h=1:length(Temp_2);
Temp_2{h}=cell2mat(Temp_2{1,h});
end
%Here we combine the cells which are made up of several %matrices and then combine them prior to forming one large %matrix for Temperature.
Temperature=[Temp_1 Temp_2];%combine the data
Temperature=cell2mat(Temperature');%create a matrix
Any help you can provide would be much appreciated.
thanks

请先登录,再进行评论。

回答(0 个)

标签

提问:

2011-11-16

Community Treasure Hunt

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

Start Hunting!

Translated by