Turning cells created with a for loop back to an array by using a for loop after doing calculations on it
12 views (last 30 days)
Show older comments
Hey guys,
when I run this code the following error occurs:
>> FileExtraction_180deg
Error using writecell (line 108)
First argument must be a cell array.
Error in FileExtraction_180deg (line 45)
writecell([timecolumn1{:}], filenameExport ,'Range', 'A5');
As in the code shown below I am extracting certain data from k .dat files. After creating the cells cellcolumn1{k} I do calculations on these extraced data. When I export the data to an excel file afterwards it seems like it is not exporting the processed data but only the raw data. I guess by creating the cells cellcolumn1{k} I am changing the class type of the data which prevents me from exporting them.
Is it possible in some way to export the processed data instead of the raw data? The code works well when I replace timecolumn1 by time1 in the last row of the code shown below.
Thank you very much for your help! :-)
Best,
Sven
clear all
format long
filenameExport = 'test.xlsx';
b_1 = [1:26];
c_1 = 26;
b_2 = [1:25];
c_2 = 25;
E = 0.059417;
t0_row = 15;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
d1_file = cell(1,c_1);
for k = b_1
d1_file{k} = ['OPTP_1_time' num2str(k) '.dat'];
end
%%%%%%%%%%%%
time1 = cell(1,numel(d1_file));
for k = 1
fileID1 = fopen(d1_file{k});
data = textscan(fileID1, '%s %s', 'HeaderLines', 16);
time1{k} = data{1};
fclose(fileID1);
end
timecolumn1 = cell(1,numel(d1_file));
for k = 1
timecolumn1{k} = vpa(time1{k});
end
for k = 1
t0{k} = timecolumn1{k}(t0_row);
end
for k = 1
timecolumn1{k} = timecolumn1{k}-t0{k};
end
writecell([timecolumn1{:}], filenameExport ,'Range', 'A5');
0 Comments
Accepted Answer
Ameer Hamza
on 12 Apr 2020
Edited: Ameer Hamza
on 13 Apr 2020
try this
writematrix(double([timecolumn1{:}]), filenameExport ,'Range', 'A5');
6 Comments
More Answers (2)
dpb
on 12 Apr 2020
Edited: dpb
on 12 Apr 2020
filenameExport = 'test.xlsx';
E = 0.059417;
t0_row = 15;
BASEFILENAME='OPTP_1_time'; % a base name as variable so can change easily w/o changing code
d=dir([BASEFILENAME '*.dcat']); % return all files of the base name to avoid having hardcode number
for k=1:numel(d) % process files
fid1 = fopen(d(k).name,'r'); % open kth file in turn
data = textscan(fid1,'%s %s', 'HeaderLines', 16);
fid1=fclose(fid1); % done with that input file
% do whatever with the returned data here, including outputting those results
...
end
Unfortunately, we don't know what the content of the input file is but looks as though probably should be read as datetime and a numeric value instead. You already have cellstr array above; by using {} you've added another nesting depth which is at least part of the problem.
While the above cleans up a bunch, I'd wager strongly using readtable or one of the other higher level input routines would be a far better/simpler solution instead of the lower-level textscan. But, we don't know the input nor the clear definition of the desired output.
Attach a sample file and clearly define the output objective...
See Also
Categories
Find more on Spreadsheets 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!