Turning cells created with a for loop back to an array by using a for loop after doing calculations on it

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');

 Réponse acceptée

try this
writematrix(double([timecolumn1{:}]), filenameExport ,'Range', 'A5');

6 commentaires

Hey Ameer,
a special thanks to you for another time helping me out. Unfortunately none of your solutions worked. I just uploaded an example file attached to this reply.
Cheers,
Sven
Based on the new information you provided, I have updated the answer.
Hey Ameer,
to be honest I don't quite understand why the code does not work. I mean both time1{k} and timecolumn1{k} are defined as cells.
Cheers Sven
can you show the error message? Also can your paste the writematrix line from your code?
Ameer...I am speechless....once again you have fixed my problem. Your updated solution works perfectly! Thank you very much! :-)
All the Best,
Sven

Connectez-vous pour commenter.

Plus de réponses (2)

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...

2 commentaires

Hey dpb,
I wantt to to extract the data in the first column or on the left of the data in the red frame. Once I have extraced these data I have to substract a certain number from each of the numbers contained in the extracted data. Afterwards I want export the processed data to an excel file.
Cheers Sven
Hey dpb,
thanks for your effort anyway. Ameer just fixed my problem.
Cheers,
Sven

Connectez-vous pour commenter.

Sven Krasel
Sven Krasel le 13 Avr 2020
Modifié(e) : Sven Krasel le 13 Avr 2020
Hey guys,
first of all thank you very for your input. I am just about to try out your solutions you provided for me. Attached you will find the an example file. Originally it is a .dat file but it is not possible to upload these kind of files here for this reason I provided a screenshot of it. For the excel file it just an empty file you can take any file.
Thanks again guys
Cheers Sven

Community Treasure Hunt

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

Start Hunting!

Translated by