Table values that is a scalar string
5 views (last 30 days)
Show older comments
I use readtable to input an Excel file into Matlab.
T1 = readtable('AA.xlsx');
Now I know T1(3,48) = '32WC20110812', but I get an error when using the below command to assign the string to app.expo.Value:
app.expo.Value = T1(3,N);
Here is the error:
Error using matlab.ui.control.EditField/set.Value (line 98)
'Value' must be a character vector or a string scalar.
Error in A01_dataLoading_Tool/LoadheaderButtonPushed (line 279)
app.expo.Value = T1(3,N);
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
What I'm missing? Thanks!
Accepted Answer
Guillaume
on 4 Jun 2019
Edited: Guillaume
on 4 Jun 2019
As said in your previous question, read the doc about tables. For this particular case, the bits about table indexing.
() indexing on a table always return a table (just as () indexing on a cell array return a cell array even if it has just one cell).
To access the content of the table use {} indexing or . indexing:
T{3, N}
%or
T{3, 'variablename'} %where variablename is the name of the Nth variable
%or
T.variablename(3) %where variablename is the name of the Nth variable
3 Comments
Guillaume
on 4 Jun 2019
If the Nth column of your table contains char vectors, it's possible that the underlying variable type is a cell array. In which case T{3, N} will be a single cell array and you need an additional {} indexing:
T{3, N}{1}
You can also use numeric indices for . indexing, in which case:
T.(N)(3)
which will also do the cell indexing for you.
Another way:
T.(T.Properties.VariableNames{N})(3) %but T.(N)(3) is simpler
More Answers (1)
per isakson
on 4 Jun 2019
Edited: per isakson
on 4 Jun 2019
On my system Win10, R2018b, Excel 2016 all the methods to reference the last column works. (I downloaded AA.xlsx from another question.)
>> T1 = readtable( 'AA.xlsx')
Warning: Variable names were modified to make them valid MATLAB identifiers. The original
names are saved in the VariableDescriptions property.
T1 =
3×48 table
STNNBR CASTNO BTLNBR BTLNBR_FLAG_W DATE Year Month Day TIME LATITUDE LONGITUDE DEPTH CTDPRS CTDTMP CTDSAL CTDSAL_FLAG_W SALNTY SALNTY_FLAG_W CTDOXY CTDOXY_FLAG_W OXYGEN OXYGEN_FLAG_W SILCAT SILCAT_FLAG_W NITRAT NITRAT_FLAG_W NITRIT NITRIT_FLAG_W PHSPHT PHSPHT_FLAG_W AMMONI AMMONI_FLAG_W TCARBN TCARBN_FLAG_W ALKALI ALKALI_FLAG_W PH_TOT PH_TOT_FLAG_W PH_TMP CHLORA PON POC POC_PON PON_1 POC_1 LINE Accession EXPOCODE
______ ______ ______ _____________ __________ ____ _____ ___ _______ ________ _________ _____ ______ ______ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ _____________ ______ ______ ____ ____ _______ _____ _____ ____ __________ ______________
1 1 1 2 2011-08-12 2011 8 12 0.59839 47.11 -126.05 2292 2309.6 1.759 34.636 2 34.633 2 75.6 2 75.61 6 185.3 2 40.02 2 0.05 2 2.89 2 0.01 2 2386.8 6 2436.3 2 7.447 3 25 -999 -999 -999 -999 -999 -999 1 1.5746e+05 '32WC20110812'
1 1 2 2 2011-08-12 2011 8 12 0.5986 47.11 -126.05 2292 2309.6 1.759 34.636 2 -999 9 72 2 -999 9 -999 9 -999 9 -999 9 -999 9 -999 9 -999 9 -999 9 -999 9 -999 -999 0.37 3.75 10.2 0.38 3.86 1 1.5746e+05 '32WC20110812'
1 1 3 2 2011-08-12 2011 8 12 0.59976 47.11 -126.05 2292 2286.3 1.759 34.636 2 -999 9 71.9 2 -999 9 185.22 2 39.98 2 0.04 2 2.91 2 0.01 2 2385.8 2 2429.4 6 7.442 3 25 -999 -999 -999 -999 -999 -999 1 1.5746e+05 '32WC20110812'
>> T1.EXPOCODE
ans =
3×1 cell array
{'32WC20110812'}
{'32WC20110812'}
{'32WC20110812'}
>> T1{:,48}
ans =
3×1 cell array
{'32WC20110812'}
{'32WC20110812'}
{'32WC20110812'}
>> T1(:,48)
ans =
3×1 table
EXPOCODE
______________
'32WC20110812'
'32WC20110812'
'32WC20110812'
>>
>> T1(3,48)
ans =
table
EXPOCODE
______________
'32WC20110812'
>> T1.EXPOCODE(3)
ans =
1×1 cell array
{'32WC20110812'}
>> T1{3,48}
ans =
1×1 cell array
{'32WC20110812'}
>>
What value does app.expo.Value have before the assignment?
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!