MATLAB: Fill Cell-Array Columns with vectors

cell arrayfillvector

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

Best Answer

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