Export a Char String to Excel

Greetings,
in my MATLAB Code (R2017a) i got a variable Matrix ('res') with the typ Char.
For example for one switch it looks like this (8x20):
Carreau-Parameter:
a = 7521.1446 Pa*s
b = 0.10637 s
c = 0.78114
Aktivierungsenergie:
E = 40281.5361 J
resnorm:
0.030751
The rows and columns of 'res' are variable and as you can see it is a String with variables in it. Therfore i would like to export every text and variable of a row (without the spaces) to a seperate column in exel. Here is an example for the last row:
A1 A2
__________ __
'0.030751' ''
First i build something like this and it worked for just one row:
dname = uigetdir('');
fileName = '\Kapillarrheometer.xlsx';
s = strcat(dname,fileName);
sheet=1;
res = ['Carreau-Parameter: ';'a = 7521.1446 Pa*s ']; % and so on...
[r,c] = size(res);
for i = 1:r
A=strsplit(res(i,:));
end
alltableres=array2table(A);
disp(alltableres);
writetable(alltableres,s,'Sheet',Sheet,'Range','C5','WriteVariableNames',0);
After this i tried to create a dynamik Matrix and it worked but isn't realy a good solution because of the varible size of 'res':
dname = uigetdir('');
fileName = '\Kapillarrheometer.xlsx';
s = strcat(dname,fileName);
sheet=1;
res = ['Carreau-Parameter: ';'a = 7521.1446 Pa*s ']; % and so on...
[r,c] = size(res);
%for i = 1:r
A=strsplit(res(1,:));
[Ar,Ac] = size(A);
if Ac<5
A=[A nan nan nan];
else
A=A;
end
B=strsplit(res(2,:)); % and so on...
allres=[A;B];
%end
alltableres=array2table(allres);
writetable(alltableres,s,'Sheet',Sheet,'Range','C5','WriteVariableNames',0);
disp(alltableres);
Here are the results of 'alltableres':
allres1 allres2 allres3 allres4 allres5
____________________ _______ ___________ _______ _______
'Carreau-Parameter:' '' [NaN] [ NaN] [NaN]
'a' '=' '7521.1446' 'Pa*s' ''
I tried to use the cellfun/arrayfun command but it didn't worked quite well. After one week of work i am very tired of this problem...
I am open for every help you can offer!
(This is my first post at this forum. Please tell me if i did something wrong with my inputs!)

4 Comments

You posted quite nicely, I just turned a little more into code blocks for legibility and so can be copied directly if somebody wants sample data...
thank you for your edit :)
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in test (line 9)
res = ['Carreau-Parameter: ';'a = 7521.1446 Pa*s ';'b = 0.10637 s ';'c = 0.78114 ']; % and so on...
Also, what is s? The filename. I assume I can just pick anything, but it would shorten the time to solution if you just gave us code that ran right out of the box.
Karl Laenger
Karl Laenger on 12 Dec 2020
Edited: Karl Laenger on 12 Dec 2020
i'm sry :/ think i missmatched something with the dimension of res. i changed it in the orginal post and the code should be working now...
yes s is the filename i add it to the orginal post...

Sign in to comment.

 Accepted Answer

The example doesn't work as is, exactly...
>> res = ['Carreau-Parameter: ';'a = 7521.1446 Pa*s ';'b = 0.10637 s ';'c = 0.78114 ']; % and so on...
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
>>
Think you'll get along a lot better if you would use cellstr instead of char() array --
>> res=strtrim([{'Carreau-Parameter: '};{'a = 7521.1446 Pa*s '};{'b = 0.10637 s '};{'c = 0.78114 '}]);
>> cellfun(@strsplit,res,'UniformOutput',false)
ans =
4×1 cell array
{1×2 cell}
{1×5 cell}
{1×5 cell}
{1×4 cell}
>> ans{:}
ans =
1×2 cell array
{'Carreau-Parameter:'} {0×0 char}
ans =
1×5 cell array
{'a'} {'='} {'7521.1446'} {'Pa*s'} {0×0 char}
ans =
1×5 cell array
{'b'} {'='} {'0.10637'} {'s'} {0×0 char}
ans =
1×4 cell array
{'c'} {'='} {'0.78114'} {0×0 char}
>>
Looks to produce what you asked for. You could eliminate the trailing null char artifact from strsplit.
To put into a regular, rectangular cell array would require augmenting shorter rows to length of the maximum.
Alternatively, if there aren't too many, you could just loop over the array to wrtie altho that will be inefficient.

4 Comments

Perfectly! This workes for me. Thank you for your fast reply!
dname = uigetdir('');
fileName = '\Kapillarrheometer.xlsx';
s = strcat(dname,fileName);
Sheet=1;
res=strtrim([{'Carreau-Parameter: '};{'a = 7521.1446 Pa*s '}]);
res1=cellstr(res);
res2=strtrim(res1);
res3=cellfun(@strsplit,res2,'UniformOutput',false);
res4=cell2table(res3);
disp(res4);
writetable(res4,s,'Sheet',Sheet,'Range','C5','WriteVariableNames',0);
for 'res4' i got this result:
res3
__________
{1×1 cell}
{1×4 cell}
if i change 'res4' as seen below i get the correct information but only for one row..
res4=cell2table(res3{1});
for 'res4' i get this result now:
Var1
____________________
'Carreau-Parameter:'
How is it possible to export all (here the 2) rows into excel for example from Range C5 till C6?
That's what was pointing out -- to be able to use this as anything but a cell array you'll have to augment the shorter cells to the size of the longest similar to what you had done above.
Or, you will have to loop through the rows writing each in turn. This may or may not be a major issue depending on how big an array you might have.
It's really not as bad to do the augmentation as sounds -- using the result from my example above where presumed save the result of the arrayfun also into variable res
>> nmax=max(cellfun(@numel,res); % return longest cell in array
>> cell2table(cellfun(@(c) [c repmat({''},1,nmax-numel(c))],out,'UniformOutput',false))
ans =
4×1 table
Var1
_________________________________________________________________________________
{'Carreau-Parameter:'} {0×0 char} {0×0 char } {0×0 char} {0×0 char}
{'a' } {'=' } {'7521.1446'} {'Pa*s' } {0×0 char}
{'b' } {'=' } {'0.10637' } {'s' } {0×0 char}
{'c' } {'=' } {'0.78114' } {0×0 char} {0×0 char}
>>
Knowing what the max size is, it's easy enough to code to add however many empty cells are needed.
Awesome now it workes how it should be!
dname = uigetdir('');
fileName = '\Kapillarrheometer.xlsx';
s = strcat(dname,fileName);
Sheet=1;
res=strtrim([{'Carreau-Parameter: '};{'a = 7521.1446 Pa*s '}]);
res1=cellstr(res);
res2=strtrim(res1);
res3=cellfun(@strsplit,res2,'UniformOutput',false);
nmax=max(cellfun(@numel,res3));
res4=cell2table(cellfun(@(c) [c repmat({''},1,nmax-numel(c))],res3,'UniformOutput',false));
writetable(res4,s,'Sheet',Sheet,'Range','C5','WriteVariableNames',0);
Thank you Sir for all your patience and i wish a nice weekend!
No problem; glad to help. Remember for a slight efficiency gain to eliminate that trailing null character cell column. Wish there were an option in strfind to tell it not to do that--never followed why TMW built it that way to begin with.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2017a

Asked:

on 12 Dec 2020

Commented:

dpb
on 13 Dec 2020

Community Treasure Hunt

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

Start Hunting!