variable names
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (3)
Fangjun Jiang
il 10 Mag 2011
To construct your file name:
for i=1:5
FileName=sprintf('A_P%d_pitchAmplitude',i);
disp(FileName);
end
2 Commenti
RoJo
il 10 Mag 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
Oleg Komarov
il 10 Mag 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 Commenti
Oleg Komarov
il 10 Mag 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)
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!