MATLAB Answers

0

How to import textdata & data from Excel spreadsheet into Struct, using for loop?

Asked by Robert Flores on 13 Sep 2019
Latest activity Edited by per isakson
on 14 Sep 2019
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

  3 Comments

Also, I just realized r is wrong. It should be r = [1:3,14:30,41:52], in regards to the Excel Spreadsheet. But, I am still only get the last row of textdata and data in all my structs.
You don't have any indexing in SBOB for 'r.' I'm assuming you need that to capture more than the final row of r.
Your code indicates that SBOB is a cell array and that each cell contains a struct. Wouldn't it be better to make SBOB a struct array?

Sign in to comment.

1 Answer

Answer by per isakson
on 14 Sep 2019
Edited by per isakson
on 14 Sep 2019

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)

  0 Comments

Sign in to comment.