user to name and save text file
9 views (last 30 days)
Show older comments
hi everyone. i have this code at the moment
for i =1:length(pts_r)
headers = {'mean_AP','Standard_dev_AP','mean_ML', 'Standard_dev_ML','resultant_pathlength','AP_velocity', 'ML_velocity'};
header_walk{i} = strcat('right_step',num2str(i));
walk{i} = table(ave_xlocation_r(i), standard_dev_AP_right(i) , ave_ylocation_r(i),standard_dev_ML_right(i),result_pathlength(i), AP_velocity(i),ML_velocity(i),'VariableNames',headers,'RowNames',header_walk(i));
end
walk = vertcat(walk{:});
walk=input('input: ','s');
writetable(walk,'RIGHTFOOT.txt','Delimiter',' ');
i am hoping to write a script that askes for the user to save the table as a text file with their own name of choice. i ideally would like a dialog box to appear prompting the user to save the table. could someone please guide me in this? :) thanks
Answers (1)
Abhishek
on 10 Jun 2025
To prompt the user to save a MATLAB table as a ‘.txt’ file with a custom filename using a dialog box, you can use MATLAB's built-in ‘uiputfile’ function to open a dialog box that lets the user choose a file name and location for saving. Once a file is selected, the ‘writetable’ function can be used to export the table.
Here is a step-by-step process to implement this workflow:
- Open the dialog box: This can be done, by calling the ‘uiputfile’ method. This opens a file save dialog, that allows the user to choose a filename. This will return two values, namely a file name and a path. Store them in respective variables.
[filename, pathname] = uiputfile('*.txt', 'Save table as');
- Write the table to the user-specified path: Using the ‘writetable’ method, the table will be written to the path specified by the user in the previous step. On the contrary, ‘uiputfile’ will return ‘0’ if the user cancels the operation. One must handle that case as well. This can be done in the following manner:
if filename
writetable(walk, fullfile(pathname, filename), 'Delimiter', ' ', 'WriteRowNames', true);
end
I implemented and ran this approach in MATLAB R2024b — below is the output for your reference:
A. Positive case: User passes the path and name for the file:

B. Negative case: User cancels the operation/ doesn’t pass the file path:

For additional information, please consult the following official documentation resources:
- ‘uiputfile’: https://www.mathworks.com/help/releases/R2024b/matlab/ref/uiputfile.html
- ‘writetable’: https://www.mathworks.com/help/releases/R2024b/matlab/ref/writetable.html
This should sort things out! Let me know if there’s anything else I can help with.
Thanks.
0 Comments
See Also
Categories
Find more on Tables 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!