Is it possible to use function for a fullfile or readmatrix data storage?

1 view (last 30 days)
I am trying to make a function in which I can access the particular path of the folder and from there, I want to use readmatrix to read the .txt file. I tried but it did not work out. Is it possible or does it make any sense to use? Or the functions can only be used for mathematical operations in Matlab.
Looking forward your response asap.
  1 Comment
Steven Lord
Steven Lord on 22 Jun 2022
What does "it did not work out" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
If you show the code as well we may be able to offer suggestions about why it didn't work as you expected and how to correct it.

Sign in to comment.

Answers (1)

Shuba Nandini
Shuba Nandini on 31 Aug 2023
I understand you want to create a function from where text file can be accessed using “readmatrix” function.
Using uigetdir” function, you can select the folder from which you want to retrieve the ‘.txt’ file. “uigetdir opens a modal dialog box that displays the folders in the current working directory and returns the path that you select from the dialog box.
Please refer to the below example on how to get the ‘.txt’ file from the specified folder:
function readtextfile() % This function reads a .txt file using readmatrix
dname = uigetdir('C:\'); % select a folder
data = readmatrix('textfile.m') %select a .txt file
Please refer to the following documentation to know more about “uigetdir” and “readmatrix” functions:
I hope this helps you to retrieve the .txt file!
Shuba Nandini
  1 Comment
Stephen23 on 31 Aug 2023
Edited: Stephen23 on 31 Aug 2023
It is insufficient to call UIGETDIR to return DNAME... which is then completely unused. In order for that code to work correctly, it requires READMATRIX to also have the path to the file.
The best approach is to use FULLFILE:
D = uigetdir('C:\');
F = fullfile(D,'textfile.m');
M = readmatrix(F);
The author also wrapped the code in a function but did not return any output argument, which is unlikely to be useful.

Sign in to comment.


Find more on Startup and Shutdown 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!