How can I assign a variable to a string?

I have a mat file containing a table one column with numbers from 1 to 50, I just want to assign avariable to first column an than apply this:
tf = ismember(M, [7; 8; 9]);
idx = find(diff(tf)>0)+1+(0:12);
answer = reshape(M(idx).', [], 1)
If I put
M = [1 2 3 4...49 50]
and then
tf = ismember(M, [7; 8; 9]);
idx = find(diff(tf)>0)+1+(0:12);
answer = reshape(M(idx).', [], 1)
I have the correct results that display only the numbers after 9 but I want to exclude the M = [1 2 3 4...49 50] and to assign a variable to entire column because my numbers are always different except 7,8,9
I tried this
M = T(:,:);
where T is the table from mat file but:
Error using table/ismember (line 27)
A and B must both be tables.
If I put directly:
tf = ismember(X, [7; 8; 9]); %X is table from mat file
idx = find(diff(tf)>0)+1+(0:12);
vals = reshape(X(idx).', [], 1);
I get this error :
Error using +
Matrix dimensions must agree.
Thanks

2 Comments

You forgot to format your code and attach your mat file. I formatted it for you but I can't attach your mat file for you.
load('simplu.mat');
M = T(:,:);
tf = ismember(M, [7; 8; 9]);
idx = find(diff(tf)>0)+1+(0:12);
answer = reshape(M(idx).', [], 1)
I attached the mat file (T is the table from mat file) still dont get it what I need to format...

Sign in to comment.

 Accepted Answer

dpb
dpb on 14 Jun 2020
Edited: dpb on 14 Jun 2020
>> whos -file simplu
Name Size Bytes Class Attributes
T - 1440 table
>> load simplu
>> whos T
Name Size Bytes Class Attributes
T 50x1 1440 table
>> T(1:5,:)
ans =
5×1 table
Var1
____
1.00
2.00
3.00
4.00
5.00
>> tf=ismember(T,[7:9]);
Error using tabular/ismember (line 37)
A and B must both be tables, or both be timetables.
>> tf=ismember(T.Var1,[7:9]);
>>
Error is correct; T is a table and [7;8;9] is a vector of double...never the twain shall meet.
As the second shows, you need to reference the variable in the table.
ADDENDUM:
Error using tabular/ismember (line 37)
A and B must both be tables, or both be timetables.
The error message is correct, as far as it goes. It was written specifically for the case of the tabular/ismember function; what it neglects to point out is that both could be numeric and the alternate ismember method would work as well...

More Answers (0)

Categories

Products

Release

R2016a

Asked:

on 14 Jun 2020

Edited:

dpb
on 14 Jun 2020

Community Treasure Hunt

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

Start Hunting!