Matlab - Arreglo de estructura de datos

 
Vista:
Imágen de perfil de Javier

Arreglo de estructura de datos

Publicado por Javier (2 intervenciones) el 16/02/2017 13:56:19
Hola, soy nuevo en esto, y estoy tratando de resolver un problema que tengo con varios equipos que me brindan datos y a partir de ellos debo realizar diversos cálculos y gráficos. El problema se me presenta a la hora de levantar los datos, uno de los equipos me genera un archivo csv, solamente separado por comas llamado 10-02-2017.

FUSTE s4, V11,
Lectura,T1,,T2,,T1-T2,,Etiqueta de tiempo
1,29,4,,29,1,,0,3,,03/02/2017 01:01:12 p.m.
2,30,0,,29,7,,0,3,,03/02/2017 01:31:12 p.m.

Inicialmente utilicé importdata, lo cual me permitió armar todo, ahora bien, intento que sea dinámico me permita levantar cualquier archivo (con el mismo contenido en forma, pero de distintas fechas y nombre de archivo) para luego trabajarlo.

Para poder abrir cualquier archivo utilicé:
D=uiimport('-file')

Cuando abro el archivo me crea una variable x100x2D020x2D2017, de 342x1, cell, dentro de una estructura, donde dentro de cada celda estan los datos que necesito.

No termino de entender como armar vectores con cada uno de los datos separados por coma, como lo hice en este otro caso:
fileID = fopen('10-02-2017.csv');
C = textscan(fileID,'%f %f %f %u8 %f %f %u8 %f %f %u8 %s','Delimiter',',','EmptyValue',0,'headerlines',2);
fclose(fileID);
c1 = C{1}, c2 = C{2} , c3 = C {3}, c4 = C {4}

O en este, donde tenía otro formato de valores:
M=importdata('03012017.csv', ',') %se leen los datos de archivo
%% TRABAJO CON TEXTOS FECHAS
%%TRABAJO CON MEDICIONES
A=[M.data];
T1a=A(:,1); %Matriz primer columna
T1b=A(:,2); %Matriz 2da columna
TBS=T1a+T1b/10; %Armo T1
T2a=A(:,3); %Matriz 3er columna
T2b=A(:,4); %Matriz 4ta columna
TBH=T2a+T2b/10; %Armo T2

Desde ya muchas gracias,
Javier.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Dave
Val: 497
Ha mantenido su posición en Matlab (en relación al último mes)
Gráfica de Matlab

Arreglo de estructura de datos

Publicado por Dave (1094 intervenciones) el 16/02/2017 16:04:01
Hola Javier;

En adjunto te envío una función y un programa que podrías usar para leer los archivos CSV y a la vez leer masivamente los archivos *.csv (siempre y cuando tengan el mismo formato).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
clear all, close all,clc
 
lst = ls('*.csv');
NV1 = [];   NV2 = [];   NV3 = [];   NV4 = [];   NV5 = [];    NV6 = [];   NV7 = [];
NV8 = [];   NV9 = [];   NV10 = [];   NV11 = []; NV12 = [];   NV13 = [];
 
for n = 1:size(lst,1)
    filename = lst(n,:);
[V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13] = leer_datos(filename);
 
NV1 = [NV1, V1];
NV2 = [NV2, V2];
NV3 = [NV3, V3];
NV4 = [NV4, V4];
NV5 = [NV5, V5];
NV6 = [NV6, V6];
NV7 = [NV7, V7];
NV8 = [NV8, V8];
NV9 = [NV9, V9];
NV10 = [NV10, V10];
NV11 = [NV11, V11];
NV12 = [NV12, V12];
NV13 = [NV13, V13];
 
end


Aquí la función para leer un archivo csv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function [V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13] = leer_datos(filename, startRow, endRow)
%LEER_DATOS Import numeric data from a text file as column vectors.
%   [V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13] = LEER_DATOS(FILENAME) Reads data from text file FILENAME for the
%   default selection.
%
%   [V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13] = LEER_DATOS(FILENAME, STARTROW, ENDROW) Reads data from rows STARTROW
%   through ENDROW of text file FILENAME.
%
% Example:
%   [V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13] = leer_datos(filename);
 
%% Initialize variables.
delimiter = {',',' '};
if nargin<=2
    startRow = 3;
    endRow = inf;
end
 
%% Format string for each line of text:
%   column1: double (%f)
%	column2: double (%f)
%   column3: double (%f)
%	column4: double (%f)
%   column5: double (%f)
%	column6: double (%f)
%   column7: text (%s)
%	column8: double (%f)
%   column9: double (%f)
%	column10: text (%s)
%   column11: datetimes (%{MM/dd/yyyy}D)
%	column12: datetimes (%{HH:mm:ss}D)
%   column13: text (%s)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%f%f%s%f%f%s%{MM/dd/yyyy}D%{HH:mm:ss}D%s%[^\n\r]';
 
%% Open the text file.
fileID = fopen(filename,'r');
 
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
    frewind(fileID);
    dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
    for col=1:length(dataArray)
        dataArray{col} = [dataArray{col};dataArrayBlock{col}];
    end
end
 
%% Close the text file.
fclose(fileID);
 
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for unimportable 
% data, select unimportable cells in a file and regenerate the script.
 
%% Allocate imported array to column variable names
V1 = dataArray{:, 1};
V2 = dataArray{:, 2};
V3 = dataArray{:, 3};
V4 = dataArray{:, 4};
V5 = dataArray{:, 5};
V6 = dataArray{:, 6};
V7 = dataArray{:, 7};
V8 = dataArray{:, 8};
V9 = dataArray{:, 9};
V10 = dataArray{:, 10};
V11 = dataArray{:, 11};
V12 = dataArray{:, 12};
V13 = dataArray{:, 13};

​Espero que sea de alguna ayuda.

Saludos
Dave Correa
[email protected]
[email protected]
Servicios de Programación Matlab
http://fismatlab.org​​
http://fismatlab.blogspot.com
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Javier

Arreglo de estructura de datos

Publicado por Javier (2 intervenciones) el 16/02/2017 23:47:57
Ante todo muchas gracias por el tiempo y sobre todo la respuesta, estuve trabajando en esto y se me presentó lo siguiente (por las dudas en R2012a) por si llega a ser parte del problema:

Error using textscan
Delimiter must be a string.
Error in leer_datos (line 43)
dataArray = textscan(fileID, formatSpec,endRow(1)-startRow(1)+1, 'Delimiter', delimiter,'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1,
'ReturnOnError', false);
Error in LecturaHR_Pruebas (line 9)
[V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13] =
leer_datos(filename);
------------------------------------

Modifique
'Delimiter', delimiter, => 'Delimiter', ','

Y obtengo:
Error using textscan
Badly formed format string.

Error in leer_datos (line 43)
dataArray = textscan(fileID, formatSpec........

Se me queman los papeles con poco...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar