I am trying to convert this column of characters:
'D48-J06-W470'
into a cell so I can append to a matrix.
I have used str2double but I keep getting errors.
I have searched online but nothing useful arose.

 Réponse acceptée

Simon
Simon le 8 Jan 2014

0 votes

Hi!
It seems you want to do:
ListCell = num2cell(List);
NewCol = size(List, 2) + 1;
for n = 1:size(Table, 1)
tf = (List(:, 1) == Table{n, 1});
ListCell(tf, NewCol) = Table(n, 3);
end

1 commentaire

T
T le 8 Jan 2014
Modifié(e) : T le 8 Jan 2014
This works !

Connectez-vous pour commenter.

Plus de réponses (3)

Azzi Abdelmalek
Azzi Abdelmalek le 3 Jan 2014

13 votes

s='D48-J06-W470'
cellstr(s)

14 commentaires

T
T le 3 Jan 2014
The following error occurred converting
from cell to double:
Error using double
Conversion to double from cell is not
possible.
Azzi Abdelmalek
Azzi Abdelmalek le 3 Jan 2014
I think this error does not concerns what I posted. Can you clarify?
T
T le 3 Jan 2014
'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'
[ NaN ]
[ NaN ]
This vector raises some issues. First it's not all in the same format. How should I declare the last two elements? Initially put {''} but I think my error arises for this reason.
A={'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'}
B=[NaN; NaN ]
out=[A;num2cell(B)]
T
T le 3 Jan 2014
I tried that. See second answer.
Azzi Abdelmalek
Azzi Abdelmalek le 3 Jan 2014
Modifié(e) : Azzi Abdelmalek le 3 Jan 2014
T your question is not asked correctly, post clearly what you have, then what you want to get.You don't need to post all your data, just a short example
T
T le 3 Jan 2014
It's because I'm not sure I know what the problem is. I suspect it has to do with those last two rows.
Azzi Abdelmalek
Azzi Abdelmalek le 3 Jan 2014
If you want to append char and double, just use cell array, you can't convert char to double, but you can convert char to cell with cellstr and double to cell with num2cell
T
T le 3 Jan 2014
Modifié(e) : T le 3 Jan 2014
Well, I think I already have a a cell
[10] [0.0273] 'D48-J11-W1076'
[11] [0.0119] 'D48-J05-W470'
[12] [0.0143] 'D48-J08-W562'
[-1] [ 0] ''
[ 0] [ 0] ''
I thought the last two columns would have been okay as presented.
I don't think I need to convert it to a cell - I'm not sure. But there's definitely a format issue.
The error I get is:
Subscripted assignment dimension mismatch.
Azzi Abdelmalek
Azzi Abdelmalek le 3 Jan 2014
Until now, I don't know what is your problem? Initially what do you have? and what do you want to obtaint?
T
T le 7 Jan 2014
Modifié(e) : T le 7 Jan 2014
Basically, I have this matrix of values called List:
List =
1.0e+004 *
0.0004 0.7886 0.0000 0.0004 0.0004 0.0000 0.0000
0.0005 0.7930 0.0001 0.0011 0 0 0
0.0006 0.7937 0.0001 0.0017 0 0 0
0.0007 0.7946 0.0001 0.0024 0 0 0
0.0008 0.7954 0.0001 0.0031 0 0 0
0.0009 0.7962 0.0001 0.0036 0 0 0
0.0010 0.7968 0.0001 0.0041 0 0 0
0.0011 0.7984 0.0000 0.0043 0 0 0
0.0012 0.7988 0.0001 0.0046 0 0 0
-0.0001 0.8015 0.0000 0.0052 0 0 0
0.0004 1.1129 0.0000 0.0061 0 0 0
I have another matrix called Table
[10] [0.0273] 'D48-J11-W1076'
[11] [0.0119] 'D48-J05-W470'
[12] [0.0143] 'D48-J08-W562'
[-1] [ 0] ''
[ 0] [ 0] ''
I want to append the third column of Table onto List.
But List is a double and the third column of Table is a cell. The values are matched with Table(:,1).
For now, the only issue I have is how to work with Table(:,3)
Azzi Abdelmalek
Azzi Abdelmalek le 7 Jan 2014
But the size of the third column should match the size of the list, where do you want to put this third column?
T
T le 8 Jan 2014
List(:,1) is an array of ID's. Table(:,1) are the ID's associated with the actual label. The idea was to assign a label in List as the 8th column.
I have been told that this is not possible because the matrix cannot not take more than one character, is this true? I may have to think of something else.
Andres Parra
Andres Parra le 19 Sep 2018
Saved my day!

Connectez-vous pour commenter.

Wayne King
Wayne King le 3 Jan 2014
Modifié(e) : Wayne King le 3 Jan 2014

2 votes

Can you be more specific, the below converts it to a cell array:
S = 'D48-J06-W470';
S = {S};
How are you using the term "cell" here?

8 commentaires

T
T le 3 Jan 2014
Sorry the matrix is a double.
Basically I have the following vector:
'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'
I am trying to append a matrix (double)
Azzi Abdelmalek
Azzi Abdelmalek le 3 Jan 2014
How this matrix is double?
T
T le 3 Jan 2014
'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'
'D48-J06-W470'
[ NaN ]
[ NaN ]
This vector raises some issues. First it's not all in the same format. How should I declare the last two elements? Initially put {''} but I think my error arises for this reason.
Image Analyst
Image Analyst le 3 Jan 2014
Modifié(e) : Image Analyst le 3 Jan 2014
This "vector" looks like it already IS a cell array. How did you get it? Did you get it from calling xlsread(), which is one way I know where you will get strings and NaNs in the same array? Do you simply want to delete the NaN cells?
T
T le 3 Jan 2014
I retrieved it from some file. This is how it appears originally.
Table =
[ 4] [0.0119] 'D48-J06-W470'
[ 5] [0.0119] 'D48-J01-W470'
[ 6] [0.0143] 'D48-J4-W562'
[ 7] [0.0143] 'D48-J3-W562'
[ 8] [0.0187] 'D48-J09-W735'
[ 9] [0.0256] 'D48-J12-W1006'
[10] [0.0273] 'D48-J11-W1076'
[11] [0.0119] 'D48-J05-W470'
[12] [0.0143] 'D48-J08-W562'
[-1] [ 0] ''
[ 0] [ 0] ''
Table(:,1) = num2cell(cellfun(@double,Table(:,1)));
Image Analyst
Image Analyst le 3 Jan 2014
Modifié(e) : Image Analyst le 3 Jan 2014
How was Table original gotten, before that line? "Retrieved from some file" is not exactly crystal clear, don't you agree? And the Crystal Ball Toolbox is still under development.
Plus, it looks like Table is already a cell, so what do you mean that you want to convert Table "into a cell so I can append to a matrix." Table is already a cell array, and one column of Table is also a cell array, a column vector where each element is a cell. Do you mean that you all 11 cells in the third column to be stuffed into one single cell instead of being in 11 separate cells? I'm not really sure you know what cells are. A cell is like a bucket, or a container. Have you ever read the FAQ? http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F like I recommended in my answer which you ignored?
T
T le 7 Jan 2014
Modifié(e) : T le 7 Jan 2014
The table was retrieved from a MS Access file. That's correct, Table is a cell.
I want to take the third column of Table, and append to a 40 x 4 matrix of type double using the ID in column 1. I have done this part, I just need to deal with the data types. I cannot simply use mat2cell as I get this error:
Warning: Single input behavior is obsolete and will be removed in a future release of MATLAB. Use
C={X} instead.
> In mat2cell at 53
In script>menu_loadFile_Callback at 198
In gui_mainfcn at 96
In script at 42
In @(hObject,eventdata)script('menu_loadFile_Callback',hObject,eventdata,guidata(hObject))
nor can I use {matrix}
T
T le 7 Jan 2014
I tried using:
cellfun(@(c_) c_ - '0', Table(index,3), 'UniformOutput', false);
but
The following error occurred converting from cell to double:
Error using double
Conversion to double from cell is not possible.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 3 Jan 2014

0 votes

Try this to concatenate cells to make a cell array:
% Create 3 sample strings (character arrays).
string1 = 'D48-J06-W470'
string2 = 'D50-J07-IA2'
string3 = 'abcdef-123456789'
% Make the first cell:
ca = {string1};
% Append strings 2 and 3 into additional cells
% so that we will have a cell array.
% We can use either of 2 different methods (or more).
ca{2} = string2; % Method #1
ca(3) = {string3}; % Method #2
% You can do it via either method.
% Display the cell array.
celldisp(ca);
Be sure to check out the FAQ http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F for a good explanation of what they are, how they work, and how to use them.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by