Export data from plot into a table *.txt

9 ビュー (過去 30 日間)
Manuel Ortiz
Manuel Ortiz 2021 年 2 月 10 日
回答済み: Riya 2025 年 3 月 3 日
I think this a very basic question, but i am new on this and i have been looking for a while and still i cannot find the answer.
I am using App Designer and I have a a function and then i polot it. Then i just want to save the data generated in a table in a text file. Let's say:
x = -5:0.1:45;
y = 4*sqrt(1 + (((x*1000) - z2)/2).^2);
plot(app.UIAxes,x,y,'r')
Now i just want to save this data on a table that you can open in a text file. I have tried this:
T = table(x,y)
writetable(T,'tabledata.txt');
type tabledata.txt
However the result is a lot of numbers with no order.. What i need is soemthing like this:
x y
1 1.2
2 2.3
3 3.4
4 4.5
Thanks in advance!

回答 (1 件)

Riya
Riya 2025 年 3 月 3 日
Hi,
I understand that you want to save the generated data in a structured text file. The issue is that “table” function requires column vectors as inputs. So, you should transpose x and y using x’ and y’. Also, you should change the delimiter of the “writetable” function from default delimiter “comma” to tab(“\t”) or space(“ “). To display the variable names “x” and “y”, set the WriteVariableNames” property to “true”.
T = table(x', y', 'VariableNames', {'x', 'y'}); % Ensure column vectors
% Write table to a text file with tab delimiter
writetable(T, 'tabledata.txt', 'Delimiter', '\t', 'WriteVariableNames', true);
This will generate a text file in the desired structure.
For more information aboutwritetable” function, refer to the following documentation:
Thanks!

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by