I have a script that I need to share with my professor. I used the commands uigetfile and uigetdir so that my professor can select my sent files for analysing but I was wondering if there was an easier way that my professor could use the scripts and if there is a way that he can write everything to a folder? I have already seen something like this : [pathname ] but I can not figure it out. Please help

6 件のコメント

OCDER
OCDER 2017 年 10 月 13 日
There may be a solution depending on what your requirements are:
  • Can you list in order the current steps taken by your professor? Ex: 1) download script, 2) copy file, 3) open matlab, 4) find + run script, 5) select file, 6) save file, 7) move file....
  • What file is this script accessing? (Or show us the script)
  • What is the script generating?
  • Do you want the script to access many different files with different names and different folder locations? uigetfile is pretty convenient, and you can set the default dir it opens to.
  • Do you have a shared folder setup on a network that's always there?
  • What is "everything" that needs to be written to a folder?
Debbie Oomen
Debbie Oomen 2017 年 10 月 13 日
There is a website where we have to upload our folder with the matlab scripts and files to be processed in the script. After uploading the professor is asked to download the folder and save it on his computer. This is the beginning of the script:
mydir = uigetdir; %selecteer folder
myfiles = dir(fullfile(mydir,'*.*'));
myfiles([myfiles.isdir]) = []; %skip . and .. and all other folders
for i=1:length(myfiles);
filename = myfiles(i).name;
[~, basename, ext] = fileparts(filename);
if isempty(basename)
fprintf('skipping dot file "%s"\n', filename)
continue; %go on to next file
end
switch ext
case {'.xls', '.xlsx'}
fprintf('xlsread for file "%s"\n', filename);
emg = xlsread(filename);
case '.csv'
fprintf('it is csvread for file "%s"\n', filename);
emg = csvread(filename);
otherwise
fprintf('Warning: file "%s" is unrecognized extension\n', filename);
continue; %go on to next file
end
fprintf('processing data with %d rows and %d columns\n', size(emg,1), size(emg,2));
%%%Plot signal
t = emg(:,1);
y1 = emg (:,2);
N=length(y1);
ls=size(y1);
f = 1/N; %frequency
fs= 1000; %sampling frequency
T = 1/fs; %period between each sample
As you can see, uigetdir will allow the professor to open the folder where a subfolder with emg files is located and can be analysed. However, I want the data T, f, N, etc.. to be written to the same folder he downloaded on HIS computer..
OCDER
OCDER 2017 年 10 月 13 日
Is the script located in HIS computer in the right folder? If so, the following can be used to determine the directory of the script:
TargetDir = fileparts(mfilename('fullpath'))
Also, how do you want to save T, f, N? as a txt, mat, csv, xlsx? What about t, y1, which are different sizes than f, fs, T, etc.?
Debbie Oomen
Debbie Oomen 2017 年 10 月 13 日
Well, the folder has two subfolders: one with the scripts and one with the emg files. t and y1 is the data that is analyzed in matlab and f,fs and T are output files that I want to save as excel file
OCDER
OCDER 2017 年 10 月 13 日
編集済み: OCDER 2017 年 10 月 13 日
I see, so you want to save the output folder to the parent directory of the script? EX:
Professor computer has:
C:/ThisDir/Script
C:/ThisDir/EMG
You want script to automatically find the EMG files, and then save results to:
C:/ThisDir/output.xlsx ? or
C:/ThisDir/Ouput/output.xlsx ?
Also, I'm assuming output.xlsx is saving T, f, fs, etc for every .emg file (so 100 emg files will give you 100 data rows) ?
Debbie Oomen
Debbie Oomen 2017 年 10 月 13 日
Yes. The professor can click on the folder with the multiple raw EMG data files (for example folder name: testsubject 1) and then let Matlab performs the analysis on every file in that folder. Then I want matlab to write all analysed data to a new excel sheet in the same folder (testsubject 1) on his computer

サインインしてコメントする。

 採用された回答

OCDER
OCDER 2017 年 10 月 13 日
編集済み: OCDER 2017 年 10 月 13 日

0 投票

Try this out and see if it works. It assumes there an "emg" subfolder. If it can't find it, then it will ask prof to select the dir. Also, "filename" is changed to the full filename. Once the loop is done, it will save all results to a summary.xlsx file in the same dir of mydir (emg directory).
%Figure out key folder paths
ScriptDir = fileparts(mfilename('fullpath')); %script subfolder
MainDir = fileparts(ScriptDir); %folder above the script folder
mydir = fullfile(MainDir, 'emg'); %emg subfolder
%Select the emg folder
if ~exist(EmgDir, 'dir') %If the default emg folder is not there, ask prof to select it
mydir = uigetdir(EmgDir);
end
if isnumeric(mydir)
error('No directory selected');
end
%Get files, not folders
myfiles = dir(fullfile(mydir,'*.*'));
myfiles([myfiles.isdir]) = []; %skip . and .. and all other folders
results = cell(length(myfiles), 1);
for i=1:length(myfiles)
%filename = myfiles(i).name; %this needs the directory too.
filename = fullfile(mydir, myfiles(i).name); %this way, you get the dir + filename
[~, basename, ext] = fileparts(filename);
if isempty(basename)
fprintf('skipping dot file "%s"\n', filename)
continue %go on to next file
end
switch ext
case {'.xls', '.xlsx'}
fprintf('xlsread for file "%s"\n', filename);
emg = xlsread(filename);
case '.csv'
fprintf('it is csvread for file "%s"\n', filename);
emg = csvread(filename);
otherwise
fprintf('Warning: file "%s" is unrecognized extension\n', filename);
continue %go on to next file
end
fprintf('processing data with %d rows and %d columns\n', size(emg,1), size(emg,2));
%%%Plot signal
t = emg(:,1);
y1 = emg (:,2);
N=length(y1);
ls=size(y1);
f = 1/N; %frequency
fs= 1000; %sampling frequency
T = 1/fs; %period between each sample
results{i} = {filename N mat2str(ls) f fs T}; %Saving results (Fixed version)
end
%Saving everything to excel in the same dir of the emg files
targetFile = fullfile(mydir, 'summary.xlsx');
xlswrite(targetfile, cat(1, results{:}));

1 件のコメント

OCDER
OCDER 2017 年 10 月 13 日
Oh, didn't realize ls was a matrix. Change this:
results{i} = {filename N ls f fs T}; %ERROR
results{i} = {filename N mat2str(ls) f fs T}; %FIXED
%but, ls will be saved something like '[1 100]'.
%Another way is to split ls into row & col components like this:
results{i} = {filename N ls(1) ls(2) f fs T}; %FIXED
NOTE: consider replacing ls with something else as this is a matlab function. Also, reserve the Answer sections for answers only, to prevent confusion as to which is an answer.
Hope this helps!

サインインしてコメントする。

その他の回答 (1 件)

Matthew
Matthew 2017 年 10 月 13 日
編集済み: Matthew 2017 年 10 月 13 日

0 投票

A simple way to do this is to use mfilename to find the path to where the file is being run on the professor's computer.
[folderPath,fileName] = fileparts(mfilename('fullpath'));
OutputPath = fullfile(folderPath,'OutputDirectory');
if ~exist(OutputPath,'dir')
mkdir(OutputPath)
end
outputFile1 = fopen(fullfile(OutputPath,'OutputFile1.txt'),'w');
fclose(outputFile1);

1 件のコメント

Debbie Oomen
Debbie Oomen 2017 年 11 月 1 日
How do I get specific parameters written to this outputFile? For example: median frequency and filename

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeText Data Preparation についてさらに検索

質問済み:

2017 年 10 月 12 日

コメント済み:

2017 年 11 月 1 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by