Load Files from User Input?
21 views (last 30 days)
Show older comments
Hello,
I am trying to load text files based on a user input.
Let's say I have files A.txt,B.txt,C.txt,D.txt, and E.txt. I would like to prompt the user, "Which file would you like to open?" and have the user input what ever file they want opened. I was thinking it would look something like this:
file = input('What file would you like to open?')
load (file)
But I cannot seem to get this to work. Any help would be appreciated.
1 Comment
Jan
on 21 Oct 2017
Prefer to explain, which problem you see, instead of "but I cannot seem to get this to work".
Answers (1)
Jan
on 21 Oct 2017
Edited: Jan
on 21 Oct 2017
file = input('What file would you like to open?', 's');
data = load(file)
A more convenient method would be uigetfile or a listdlg.
FileList = {'A.txt', 'B.txt', 'C.txt', 'D.txt', 'E.txt'};
[s,v] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString', FileList);
4 Comments
Garvit Amipara
on 9 Apr 2021
Edited: Garvit Amipara
on 9 Apr 2021
I am new to matlab and have a question related to the same topic.
It is necessary to use the input arguments to load different .mat files(2xn matrix) each time the function is used by user. The number of .mat files to be processed inside the function are also changing, so I used varargout and varargin. For each input, there are 2 output vectors. As input arguments in varargin can be called by varargin{1}, varargin{2}... I tried to use it to load the files. It did not work.
For example, 2 files that should be executed are Data1.mat, Data2.mat
Outputs will be Ans1,Ans2,Ans3,Ans4.
So I used the following command and function file(saved as Data_function.m)
[Ans1,Ans2,Ans3,Ans4]= Data_function (Data1, Data2)
function varargout = Data(varargin)
A= load ('varargin{1}'); %to load first inputargument file name
Ans1 = A(:,1).*3; %just for example
Ans2 = A(:,2).*4;
B= load ('varargin{2}') %to load first inputargument file name
Ans3 = A(:,1).*3;
Ans4 = A(:,2).*4;
end
This gives an erro: Unable to read file 'varargin{1}'. No such file or directory.
(The files Data1 and Data2 are in the current folder, where the function is saved)
It would be a great help if you could show the right way to load multiple files.
Jan
on 9 Apr 2021
Replace
A= load ('varargin{1}');
by
A= load (varargin{1});
In the first case, Matlab tries to load a file with the name "varargin{1}.mat".
See Also
Categories
Find more on Workspace Variables and MAT Files 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!