Fill Cell-Array Columns with vectors
2 views (last 30 days)
Show older comments
Hey, I am working on a code of someone else, trying to modify it for my own purpose but I am stuck on some points. I am hoping someone can help me with this. In generall I am trying to import a .xls-file, consisting of strings and doubles, replace some of the characters there (because german language) and fill an SQL-Database with this data in the right chronological order.
My problem right now: I imported with xlsread my file in txt since i can use strrep on strings only and made a cell array to fill
[num,txt,raw] = xlsread(filename);
GivenData= cell(size(raw,1),size(raw,2));
after that I made adjustments to replace the characters matlab cant handle and wrote them in GivenData. Now GivenData is filled with the right strings but the numbers from my xls-file are missing. How can i put them in GivenData?
spalteAuftrag = num(:,1);
spalteKavitaet = num(:,2);
spalteWerkzeug = num(:,3);
spalteTeil = num(:,5);
spalteRezeptur = num(:,7);
I want these vectors to fill GivenData.
for example spalteAuftrag should fill the empty space in the 3rd column, spalteKavitaet the 4th spalteWerkzeug the 5th and so on. This does not seem to be an impossible job but still i cant get usefull results. with
GivenData(:,3)={spalteAuftrag}
it just fills every cell in my third row with to whole vector. Can someone help me with this?
P.S. english is not my first language, so I hope I could explain the problem well enough. P.P.S I am using Matlab R2016a
0 Comments
Accepted Answer
Guillaume
on 22 Jul 2017
Edited: Guillaume
on 22 Jul 2017
Since you're using R2016a, why aren't you using readtable instead of the archaic xlsread? This will read your numeric columns as numeric and text ones as text (and if you were on R2016b or later, you could even tell readtable how to parse each column if it doesn't do it right).
Otherwise, your problem is that you need to convert the numeric column you want to insert in the cell into a cell array itself. You use num2cell for that:
[num,txt,raw] = xlsread(filename);
GivenData= cell(size(raw)); %simpler syntax than what you were using
GivenData(:, 3) = num2cell(num(:, 3)); %for example
But really, consider using readtable which is a lot more powerful and easier to work with than xlsread.
3 Comments
Guillaume
on 22 Jul 2017
But first i just want this code to run
and most likely it would run with just one line:
GivenData = readtable(filename);
which will read the headers, numbers as numbers, dates as dates, text as text, etc. without you doing anything.
How can i tell matlab to fill from the 2nd row to the last and just leave the first as it is?
The same way you tell matlab to fill anything from the 2nd row to the end: using indices that start at 2:
GivenData(2:end, 3) = ... %start filling at row number 2.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!