I have a 100001x2 cell. Each cell is 1x1.
How can I convert it to matrix? I've tried cell2mat but I can't make it work

5 comentarios

Jon
Jon el 10 de En. de 2022
You don't provide enough detail for anyone to figure out how to help you with your problem. Please provide a self contained example, that reproduces the problem, and also let us know exactly what error messages you are getting
Torsten
Torsten el 10 de En. de 2022
We can't tell - usually it should work with cell2mat. Are you sure each cell contains a numeric value ? Can you share your code for testing ?
%Import data
data=uigetfile('*.dat','Select first baseline, second signal','MultiSelect', 'on');
signal=importdata(data{1,2});
signal=cell2mat(signal.textdata([6:end],[1,2]));
I've attached the file (the original one is .dat)
I can't find the error.
Matt J
Matt J el 10 de En. de 2022
The code you've shown does not produce a 100001x2 cell array. You should attach the cell array dirctly in a.mat file so that we can avoid the steps of generating it.
ca = signal.textdata([6:end],[1,2]);
save('answers.mat', 'ca');
Then attach answers.mat with the paperclip icon.

Iniciar sesión para comentar.

 Respuesta aceptada

Jon
Jon el 10 de En. de 2022
Side stepping the issue a little bit, I can read in your data file perfectly as numerical values and not use cell2mat at all using:
A = readmatrix('i1uss.txt','NumHeaderLines',5)

3 comentarios

Jon
Jon el 10 de En. de 2022
The reason cell2mat doesn't work is that the variable
signal.textdata([6:end],[1,2])
is a cell array of quoted numerical values, e.g.
{'-9.8e-07' } {'-0.0202357212'}
{'-9.799e-07'} {'-0.0202313547'}
{'-9.798e-07'} {'-0.0202269882'}
cell2mat doesn't know those are numbers and tries to make them into character arrays which don't have similar numbers of columns which gives a concatenation error.
You can just use:
signal = str2double(signal.textdata([6:end],[1,2]))
which seems to work fine
Manuela Maria ALAMPI
Manuela Maria ALAMPI el 11 de En. de 2022
It works! Thank you so much
Matt J
Matt J el 11 de En. de 2022
@Manuela Maria ALAMPI since it worked, you should Accept-click the post.

Iniciar sesión para comentar.

Más respuestas (2)

Matt J
Matt J el 10 de En. de 2022
cell2mat should have worked
A=num2cell(rand(100001,2)); whos A
Name Size Bytes Class Attributes A 100001x2 22400224 cell
B=cell2mat(A); whos B
Name Size Bytes Class Attributes B 100001x2 1600016 double
Simon Chan
Simon Chan el 10 de En. de 2022
Try this, where A is your 100001x2 cell:
cell2mat(cellfun(@(x) x,A))

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 10 de En. de 2022

Comentada:

el 11 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by