How can I access files from a matlab directory using a user input to select the file to plot the data? Using a For Loop
1 view (last 30 days)
Show older comments
I have a .exe file while I can select the name of the folders I want to get the data from.
Then I get this by means of a user input and i get that in a .txt file.
I want to read the input of that .txt file and load the .mat file or workspace.mat from the specific folders that were selected.
Then maybe I only want channel one to be plotted or so on, but always according to the input.
But when I try to load, in a for loop, I am not able t come up with a solution for my problem.
Please Help.
cd('results')
[status,cmdout]=system('get_file_names');
fid_sel=fopen('my_selection.txt');
foldernames=[];
a=fgetl(fid_sel);
idx=1;
while(ischar(a))
foldernames{idx}=a;
a=fgetl(fid_sel);
idx=idx+1;
disp(a);
end
fclose(fid_sel);
cd ..
% Channel selection for printout
CHSEL= [1,0,0]; %[Ch1, CH2, CH3] Channel selection, 1..selected, 0.. not selected
% loop over foldernames and get the .mat files and print this.
for kk=1:length(foldernames)
filename = sprintf ('\workspace.mat')
S = load(filename)
load(foldernames,'\workspace.mat') % mat file from the folder in foldernames
ydata=[];
if (CHSEL(1))
ydata(:,1) %= first channel from the loaded data
end
load('\workspace.mat','ydata')
if (CHSEL(2))
ydata(:,2) %= second channel from loaded data
end
load('\workspace.mat','ydata')
if (CHSEL(3))
ydata(:,3) %= third channel from loaded data
end
load('/workspace.mat','ydata')
plot() % plot the graph
hold on
h1=figures(1);
hold off;
title('Amplitude peaks over frequency')
legend({'RX1','RX2','RX3'})
xlabel('f_C_W (GHz)')
ylabel('Peak Amplitudes (dB_F_S)')
plot (y)
% title
% legend...
% ...
end
5 Comments
Answers (1)
Jan
on 27 Nov 2018
Edited: Jan
on 27 Nov 2018
The messages are clear: "Warning: Escaped character '\w' is not valid. See 'doc sprintf' for supported special characters." and "File name is empty". This shows, that the creation of the file name failed and the problem is not caused by the contents of the files.
The line:
filename = sprintf ('\workspace.mat')
replies an empty filename. Use:
% either
filename = 'workspace.mat';
% or
filename = sprintf('%s', '.\workspace.mat');
Starting a file name with a backslash is a strange idea. Do you mean the file in the current directory? Then use either '.\' as start of the file name or simply omit the path completely.
I do not know what ".workspace format" means. Do you mean .mat files?
"Look like this: 57,5000000000000 57,5500000000000 57,6000000000000 57,6500000000000 ..." is not useful here. By the way: Matlab uses dots, not commas, as decimal separator.
I guess, you want something like this:
for kk = 1:length(foldernames)
% Use absolute path instead of relative file name:
filename = fullfile(foldernames{kk}, 'workspace.mat'));
ydata = load(filename, 'ydata')
...
end
See Also
Categories
Find more on File Operations 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!