Asked by Robert Flores
on 13 Sep 2019 at 14:25

Hello,

I am trying to insert some data I have into a new varible that is made up of structs. I think my for loop is working somewhat correctly now. However, it is only giving me the last row of data from my Excel sheet, in each struct. I think it has to do with my indexing of my new varible SBOB, I do not know. If anyone could help me out in resolving this issue, it will be greatly appreciated.

Sincerely,

Robert

CODE:

NOTE: SBOB_UD comes from the BOB mat file, and I define SBOB as SBOB = SBOB_UD;

clc, clear, close all

zpcstartup

%% Data of Interest

% These for loops were used in creating SBOB & SBridge

% % Let this be for BOB

% for i = [12:14,44:60,71:82]

% filename1{i} = sprintf('Z:\\BARB\\TestData\\ShockData_July2018\\Time_Histories_R2\\test0%d_filt_time.mat',i);

% SBOB{i} = load(filename1{i})

% end

% % Let this be for Bridge

% for i = [15:19,31,34,41:43,61:70,83:93]

% filename2{i} = sprintf('Z:\\BARB\\TestData\\ShockData_July2018\\Time_Histories_R2\\test0%d_filt_time.mat',i);

% SBridge{i} = load(filename2{i})

% end

load('DropTable_BOB_filtdata.mat')

load('DropTable_Bridge_filtdata.mat')

%% Decimate the data to a sample rate of 1e5

% save it in a new variable called SBOB

for p = 1:length(SBOB)

for r = 1:63

SBOB{p}.Title.Direction = excel.textdata{r,1};

SBOB{p}.Title.Item = excel.textdata{r,2};

SBOB{p}.Title.DropHeight = excel.textdata{r,3} % inches

SBOB{p}.Title.ShockG = excel.data(r,6); % G

SBOB{p}.Title.ShockT = excel.data(r,7); % msec

SBOB{p}.Title.Test = SBOB_UD{p}.title1(8:end); % This guy says what test one is looking at

SBOB{p}.labels = SBOB_UD{p}.labels; % Accelerometers

SBOB{p}.Acc = zeros(20001,16);

SBOB{p}.tt = zeros(20001,1);

end

% Decimate unfiltered data

for h = 1:16

SBOB{p}.Acc(:,h) = decimate(SBOB_UD{p}.accel_data(:,h),25);

SBOB{p}.tt = decimate(SBOB_UD{p}.t_data(:,1),25)/1000; % divide by 1000 to convert to sec

end

end

Answer by per isakson
on 14 Sep 2019 at 3:53

Edited by per isakson
on 14 Sep 2019 at 5:40

What is the size of SBOB ? The for-loop

% % Let this be for BOB

% for i = [12:14,44:60,71:82]

% filename1{i} = sprintf('Z:\\BARB\\TestData\\ShockData_July2018 ...

% SBOB{i} = load(filename1{i})

% end

indicates that SBOB is a vector and that each cell contains a scalar struct.

However, to make sense the nested for-loops

for p = 1:length(SBOB)

for r = 1:63

SBOB{p} ...

...

end

end

requires that

- either SBOB is an <63 x length(SBOB)> array, not a vector
- or the contained struct is a vector of length 63, not a scalar.

and that SBOB{p} would be either SBOB{r,p} or SBOB{p}(r)

Or maybe more likely the values of the fields Direction, Item, DropHeight, ShockG, ShockT, are vectors of length 63. If that's the case, SBOB{p}.Title.ShockG would be replaced by SBOB{p}.Title.ShockG(r)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Robert Flores (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480280-how-to-import-textdata-data-from-excel-spreadsheet-into-struct-using-for-loop#comment_745629

## Bob Nbob (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480280-how-to-import-textdata-data-from-excel-spreadsheet-into-struct-using-for-loop#comment_745750

## per isakson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480280-how-to-import-textdata-data-from-excel-spreadsheet-into-struct-using-for-loop#comment_745832

Sign in to comment.