variable names

7 vues (au cours des 30 derniers jours)
Luc Nijs
Luc Nijs le 10 Mai 2011
Hi there, i'm quite new to matlab and I'm trying to process some data I have in several textfiles.
These textfiles have names that are very similar.
e.g. A_P1_pitchAmplitude A_P2_pitchAmplitude A_P3_pitchAmplitude A_P4_pitchAmplitude A_P5_pitchAmplitude B_P1_pitchAmplitude B_P2_pitchAmplitude B_P3_pitchAmplitude B_P4_pitchAmplitude B_P5_pitchAmplitude C_P1_pitchAmplitude C_P2_pitchAmplitude C_P3_pitchAmplitude C_P4_pitchAmplitude C_P5_pitchAmplitude
I suppose it most be possible to make some kind of a loop in which the name of the datafile is variabale. In this way I could make my code more simple instead of having to copy-paste the same code for each file. Can somebody help me on how to do this?
This is what I want to do to the different files:
A_P1_pitchMinFile = importdata('A_P1_pitchMin.txt');
A_P1_pitchMin = A_P1_pitchMinFile(1,2); %minimale pitch
A_P1_pitchMaxFile = importdata('A_P1_pitchMax.txt');
A_P1_pitchMax = A_P1_pitchMaxFile(1,2); %maximale pitch
A_P1_pitchAmplitude = importdata('A_P1_pitchAmplitude.txt');
A_P1_pitch = A_P1_pitchAmplitude(:, 1:2);
for i = 1 : length(A_P1_pitch(:,1))
if A_P1_pitch(i,2) < A_P1_pitchMin
A_P1_pitch(i,2) = NaN;
end
if A_P1_pitch(i,2) > A_P1_pitchMax
A_P1_pitch(i,2) = NaN;
end
end
Thank you! Kind regards, Luc

Réponses (3)

Fangjun Jiang
Fangjun Jiang le 10 Mai 2011
To construct your file name:
for i=1:5
FileName=sprintf('A_P%d_pitchAmplitude',i);
disp(FileName);
end
  2 commentaires
Luc Nijs
Luc Nijs le 10 Mai 2011
Thank you! This works. Now I have to be able to also change the 'A' into 'B' and 'C'.
Oleg Komarov
Oleg Komarov le 10 Mai 2011
Did you try my version?

Connectez-vous pour commenter.


RoJo
RoJo le 10 Mai 2011
Not sure this is what you mean, but as a start.
You can use dir function to read in all the files in a directory
files = dir('C:\Test'); % Or whatever is your directory with the files
You then have a struct array where you can access each filename by
for i=3:size(files, 1)
fileName = strcat('C:\Test\', files(i).name);
a = importdata(fileName);
end
  1 commentaire
Luc Nijs
Luc Nijs le 10 Mai 2011
Thank you!
This is a way to prevent that files have to be in the "current directory"? And now it just loads al the files in the directory, but what If I want to select certain files from that directory?

Connectez-vous pour commenter.


Oleg Komarov
Oleg Komarov le 10 Mai 2011
Variable parts of the name
num = '1':'5';
let = 'A':'C';
Combine the variable parts
nNum = numel(num);
nLet = numel(let);
let = repmat(let,nNum,1);
num = repmat(num,1,nLet);
Build subnames
subn = reshape(sprintf('%c_P%c_pitch',[let(:).'; num]),[],nNum*nLet).';
Process each set of three files (amplitude, min, max)
for f = 1:size(subn,1)
% Import
data.(subn(f,:)) = importdata([subn(f,:) 'Amplitude.txt']);
data.([subn(f,:) 'Min']) = importdata([subn(f,:) 'Min.txt']);
data.([subn(f,:) 'Max']) = importdata([subn(f,:) 'Max.txt']);
% Set min < MIN = NaN
idx = data.(subn(f,:))(:,2) < data.([subn(f,:) 'Min'])(1,2);
data.(subn(f,:))(idx,2) = NaN;
% Set max > MAX = NaN
idx = data.(subn(f,:))(:,2) > data.([subn(f,:) 'Max'])(1,2);
data.(subn(f,:))(idx,2) = NaN;
end
Are you sure that:
pitch(:,2) < min(1,2) --> pitch(:,2) = NaN
pitch(:,2) > min(1,2) --> pitch(:,2) = NaN
what contains the first column of pitch?
  2 commentaires
Luc Nijs
Luc Nijs le 10 Mai 2011
Thank you!
I've tried it but i get an error message, stating:
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
I'm trying to find out what I did wrong...
the first column of pitch is a timestamp. Here I don't realy need it. I just want to "clean" the dataset, in which values below the pitch minimum (lowest not an the instrument that was recorded) and above the pitch maximum (highest note played) are considered to be errors in the pitch tracking.
Oleg Komarov
Oleg Komarov le 10 Mai 2011
Enclose the code into (commenting out with % the comments):
function foo
num = ...
...
for
...
end
end
Save the function somewhere and launch it from the cmd window:
foo
Then report the full message error (especially the line where the error occurs)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by