loop

2 visualizaciones (últimos 30 días)
ricco
ricco el 10 de Nov. de 2011
I have a script which reads .dat files into matlab. I have written the script to work for any year, as in the specific year that you type in the beginning. I am just wondering how to change to code so that I can use it for more than one year of data, my code is:
files = dir('*.dat');
Year=input('year:');%which i use 2007
%use the number of files to run a loop to import the relevant data
for i=1:length(files);
File_Name{i}=files(i,1).name;%Removes the file names from 'files'
data{i}=importdata(File_Name{i});%import the data selected by the file
%names given in 'File_Name'.
data{i}=data{i}(1:2:end,:);%this takes every other row of data(i),
%starting with the first.
data{i}=data{i}(:,2:end);%this just re-defines the matrix so that
%every row is kept and every column from the second onwards.
%CONVERT DATA INTO ONE MATRIX
data1=cell2mat(data');
%categorise the data so that we have one variable defining each year
Y_data=find(data1(:,1) == Year);
Y_data=data1(Y_data,:);
end
So, at the minute Y_data is a matrix for the data from 2007. I want Y_data to give me 3 matrices, one for 2007, one for 2008, and the other for 2009. I want to know how it would be possible to say in the beginning that Year can be equal to more than one year so that during the end of the loop you can create more than one matrix, if that makes sense.
cheers

Respuestas (1)

ricco
ricco el 10 de Nov. de 2011
failing this, how is it possible to use mat2cell but define which values I want in each cell. The matrix I have has the first column for year so how would I use mat2cell to have a cell which made up of three different years; i.e. im guessing first find the values in the matrix corresponding to each year and then place those values in different cells?
cheers
  1 comentario
Fangjun Jiang
Fangjun Jiang el 10 de Nov. de 2011
help mat2cell

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by