How can a make a matrix which keeps on updating itself and after the loop is over its saves it as a cvs file?
    4 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I am trying to make a matrix which keeps on updating itself with the value of coefficients(in my code) in a for loop and after the loop is over it save the matrix as a cvs file. Below is my code. Any help would be appreciated.
    for k=1:100
    data=sprintf('%d_1.csv',k); 
    com_data=load(data); 
    com_data=load('1_1.csv');
    voltage=com_data(:,2);
    voltage_freq=fft(voltage);      
    voltage_freq_mag=abs(voltage_freq);
    threshold=voltage_freq_mag(29);
    a=find(voltage_freq_mag >= threshold);
    for i = a
        voltage_edit=voltage_freq(i);
    end
    coefficients=voltage_edit(1:9)
    csvwrite('set1',coefficients)
end
0 Commenti
Risposta accettata
  dpb
      
      
 il 22 Giu 2017
        coefficients=zeros(100,9);            % preallocate
for k=1:100                           % stylistic hint:  use variables for limits
  fname=sprintf('%d_1.csv',k);        % more descriptive variable name
  com_data=load(fname); 
  voltage_freq=fft(com_data(:,2));      
  voltage_freq_mag=abs(voltage_freq); 
  threshold=voltage_freq_mag(29);     % use variables for "magic numbers"
  voltage_edit=voltage_freq(voltage_freq_mag >= threshold);  % use logical addressing vector directly
  coefficients(k,:)=voltage_edit(1:9); % populate array, magic number again...
end
csvwrite('set1.csv',coefficients)      % write full array
Using the above style comments this could be something like
nFiles=100;                           % number files to process
nCoeff=9;                             % number fft coefficients to save
idxThresh=29;                         % threshold peak index
coefficients=zeros(nFiles,nCoeff);    % preallocate
for k=1:nFiles
  fname=sprintf('%d_1.csv',k);
  com_data=load(fname); 
  voltage_freq=fft(com_data(:,2));      
  voltage_freq_mag=abs(voltage_freq); 
  threshold=voltage_freq_mag(idxThresh);
  voltage_edit=voltage_freq(voltage_freq_mag >= threshold);
  coefficients(k,:)=voltage_edit(1:nCoeff);
end
csvwrite('set1.csv',coefficients)      % write full array
I wonder if the 1:nCoeff are really those you want; the Matlab fft returns the two-sided spectrum and the center frequency is in center. You're picking the first N largest most negative frequency components here...is that what you're really after?
Also, you're using a "magic number" of 29 for the channel; wouldn't it be more robust to use findpeaks or other searching to actually locate the peak in the signal? Can you be assured it is always going to be at element 29?
Another stylistic comment, instead of building a filename as a string, consider using dir and a wildcard in the filename pattern and then iterating over the returned directory structure...
d=dir('*_1.csv');      % all .csv files with a name ending with _1 
for k=1:length(d)      % iterate over all files found
  com_data=load(d(i).name);   % load each in turn
  ...
is quite a bit simpler to code.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su File Operations 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!

