Reading cell array from excel table, which contains different sizes of doubles
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes

I would like to read these columns as cell arrays, where the components are doubles, such as the following:
m_con = {[1,2,3],2,3};

I have tried xlsread but it didn't work.
Accepted Answer
There might be a smoother method but this works with the data from your image.
file = 'Book1.xlsx';
opts = detectImportOptions(file);
opts = setvartype(opts, 'char');
C = readcell(file,opts); % you can also try readtable()
numIdx = cellfun(@isnumeric,C);
C(numIdx) = cellfun(@num2str,C(numIdx),'UniformOutput',false);
Cnum = cellfun(@str2num,C,'UniformOutput',false)
Cnum =
3×4 cell array
{1×3 double} {1×3 double} {[ 1]} {[ 1]}
{[ 2]} {[ 2]} {1×2 double} {1×2 double}
{[ 3]} {[ 2]} {1×2 double} {1×2 double}
Cnum{1,1}
ans =
1 2 3
8 Comments
Ege Arsan
on 12 Jan 2021
Thank you so much!
Ege Arsan
on 12 Jan 2021
By the way, when i try to import from a spreadsheet C returns as 3x3. Do you know where the problem could be?
C = readcell(filename,'Sheet','Table2',opts);
Ege Arsan
on 12 Jan 2021
I meant like this
C = readcell(filename,opts,'Sheet','Tabelle2');
Adam Danz
on 12 Jan 2021
I would need the file to investigate.
Ege Arsan
on 12 Jan 2021
I have figured that problem but came up with a new one. I have the following table from another file and i guess bucause of the empty spaces in the table i get an error like this: Error using str2num (line 35) Input must be a character vector or string scalar.

Is there a way to ignore the empty spaces and have the first two as 2x1 arrays?
Adam Danz
on 12 Jan 2021
This line from my answer identifies cell elements that are already numeric and is used to copy those values to the output cell array.
numIdx = cellfun(@isnumeric,C);
You can add an additional line that search for character vectors.
charIdx = cellfun(@ischar,C);
Cnum(charIdx) = cellfun(@str2num,C(charIdx),'UniformOutput',false)
*not tested
Ege Arsan
on 12 Jan 2021
It works! Thank you so much for your help!
Ege Arsan
on 12 Jan 2021
I have one last quastion. I tried to combine these two with an if condition but i still get en error. Do you know a better expression or is it completely false waht i did?
if ~isnumeric(C)
numIdx = cellfun(@isnumeric,C);
C(numIdx) = cellfun(@num2str,C(numIdx),'UniformOutput',false);
Cnum = cellfun(@str2num,C,'UniformOutput',false);
elseif ~ischar(C)
charIdx = cellfun(@ischar,C);
Cnum(charIdx) = cellfun(@str2num,C(charIdx),'UniformOutput',false);
end
More Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)