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)
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
Luciana Noronha
Luciana Noronha on 27 Nov 2018
I tried seeing some code online, but it says empty char array and I want to access all the data that looks like this in variables stored in .workspacemat. I have multiple folders with different variables and the results look like this:
57,5000000000000 57,5500000000000 57,6000000000000 57,6500000000000 57,7000000000000 57,7500000000000 57,8000000000000 57,8500000000000 57,9000000000000 57,9500000000000 58 58,0500000000000 58,1000000000000 58,1500000000000 58,2000000000000 58,2500000000000 58,3000000000000 58,3500000000000 58,4000000000000 58,4500000000000 58,5000000000000 58,5500000000000 58,6000000000000 58,6500000000000 58,7000000000000 58,7500000000000 58,8000000000000 58,8500000000000 58,9000000000000 58,9500000000000 59 59,0500000000000 59,1000000000000 59,1500000000000 59,2000000000000 59,2500000000000 59,3000000000000
Luciana Noronha
Luciana Noronha on 27 Nov 2018
I want a for loop that access the data based on the data selection program and then I can access the folder selected in the Data Selection and plot the data.

Sign in to comment.

Answers (1)

Jan
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

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!