I keep on getting the error conversion to double from table is not possibl

23 views (last 30 days)
Dear,
I am new in matlab and started writing a program to import data into a Matrix
I used readtable fuction to upload my data in a table. Then I the detectImportOptions to see how matlab interpids the data.The table is made up of 9 columns of numbers ( the first row is as followed: 2023 9 30 11 0 0.2200 1.1600 18.200 86.7900) . Then I want to use the data in the matrix for example printing to a string:
m_tab =readtable("data_wave_bouy2.txt");
m_strDateTime1=sprintf('%s-%s-%s %s:%s:00',m_tab2(1,3),m_tab2(1,2),m_tab2(1,1),...
m_tab2(1,4),m_tab2(1,5));
here I keep on getting the error 'Conversion to double from table is not possible'.
How can this be solved ?
So Now I forced the data to be read as a string and try to build a string from the table. See code below:
opts = detectImportOptions("data_wave_bouy2.txt");
opts1=opts;
opts1=setvaropts(opts1,'type','string');
m_tab2=readtable("data_wave_bouy2.txt",opts1);
opts1=setvaropts(opts1,'type','string');
m_tab2=readtable("data_wave_bouy2.txt",opts1);
str1=sprintf('%s %s %s',m_tab2(1,1),m_tab2(1,1),m_tab2(1,1));
the code simply keeps on returning with the error in the sprintf.
This looks a bit strange because the data is now as string so it should be possible to use the sprintf fuction.
can you give me some insight on what I am doing wrong.Thanks
Hans
  1 Comment
Stephen23
Stephen23 on 5 Dec 2024 at 15:13
Edited: Stephen23 on 5 Dec 2024 at 16:20
"can you give me some insight on what I am doing wrong."
You are using parentheses (), which return a subset of the table, i.e. another table. But SPRINTF does not accept tables as its inputs (it accepts numeric or text types). Thus the error.
If you want to access the table content then use curly braces {}.
In any case, do not reinvent the wheel: https://www.mathworks.com/help/matlab/ref/datetime.html

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 5 Dec 2024 at 13:29
I am not certain that I understand what you are doing.
It would probably help to upload the matrix (text or Excel file) to here using the ‘paperclip’ icon in the top toolstrip. It would then be availablte to work with here.
That aside, use curly brackets {} to get data from a table:
m_strDateTime1=sprintf('%s-%s-%s %s:%s:00',m_{tab21,3},m_tab2{1,2},m_tab2{1,1},...
m_tab2{1,4},m_tab2{1,5});
The datetime function can probably do all this for you relatively easily.
.

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!