Export data from plot into a table *.txt

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일

0 개 추천

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!

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

질문:

2021년 2월 10일

답변:

2025년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by