MATLAB Manipulacion de Archivos Excel

download MATLAB Manipulacion de Archivos Excel

of 6

Transcript of MATLAB Manipulacion de Archivos Excel

  • 7/24/2019 MATLAB Manipulacion de Archivos Excel

    1/6

    MATLAB: Crear carpetas, crear yguardar archivos (.txt), figuras (.fig),

    hojas de excel (.xls) y el or!spaceAlgu"as ocasio"es puede resultar #til, el guardar y orga"i$ar los datos

    auto%&tica%e"te. 'ara esto puede ser "ecesario el crear y ad%i"istrar carpetas y

    archivos de difere"tes tipos (figuras, .txt, excel, etc) por %edio de co%a"dos e"

    MatLa. Al fi"al de este post se prete"de ote"er u" script ue cree tres carpetas

    (experiencia1, experiencia2, experiencia3) ue se e"cue"tre" alojadas e" u"a carpeta

    ge"eral deRESULTADOS, ade%&s e" cada carpeta de experie"cias, se guardar&:

    *" archivo de texto (readme.txt)co" i"for%aci+" de la si%ulaci+",

    *"a figura (si".fig),

    Toda la i"for%aci+" o variales del ordspace (wordspace.mat),

    *"a variale i"depe"die"te, caso - se reuiera todas las variales (si".%at)

    y

    *" archivo de excel (hoja.xls).

    Los resultados descritos se puede" apreciar e" lafi!ra 1.

    Figura 1."!rrent #o$der

  • 7/24/2019 MATLAB Manipulacion de Archivos Excel

    2/6

    %Obtener el Path de la carpeta Actual

    [stat,struc]= fleattrib;

    PathCurrent = struc.Name;

    orn=!"

    %crear las rutas #Path$ para carpetas archi&os

    'olderName = [(e)periencia(num*str#n$];

    Path'older = [PathCurrent (+-/01A2O+('olderName];

    Namee)cel = [Path'older (+ho3a.)ls(];

    % operaciones del pro4rama

    ) = 5pi!.!pi;

    = sin#n6)$;

    % 4rafcar

    sin = f4ure;

    set#sin,(Name(,['olderName (7sin(],(Number1itle(,(o8($;

    plot#),$;

    4ridon;

    title#(sin#)$($

    le4end#( = sin#)$($;

    )label#()($; label#(sin#)$($;

    % crear las carpetas para 4uardar los resultados

    m9dir#[PathCurrent (+-/01A2O(], 'olderName$;

    %:uardar datos en archi&o -C-0

    titulo = rite#Namee)cel, titulo, , (A($;

    )ls>rite#Namee)cel, datos, , (A*($;

    end

    :0OA?O!Path older! carpeta de la rutaPath Current! Actual camino+ Actual direcci@n

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/sin.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/grid.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/title.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/legend.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/ylabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/mkdir.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/sin.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/grid.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/title.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/legend.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/ylabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/mkdir.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.html
  • 7/24/2019 MATLAB Manipulacion de Archivos Excel

    3/6

    Codi4o completo!

    %% Crear carpetas, crear 4uardar archi&os .t)t, .f4, .)ls >or9space

    % 6666666666666666666666666666666666666666666666666666666666666666666666666

    % Autor! 'rancisco A. ando&al N.

    % Creati&e Commons 0icense! CC B5NC5N2 ".

    % asando&alnD4mail.com

    % "+E+**

    %6666666666666666666666666666666666666666666666666666666666666666666666666

    clc

    clearall

    tic%para mostrar tiempo de simulaci@n

    %Obtener el Path de la carpeta Actual

    [stat,struc]= fleattrib;

    PathCurrent = struc.Name;

    orn=!"

    %crear las rutas #Path$ para carpetas archi&os

    'olderName = [(e)periencia(num*str#n$];

    Path'older = [PathCurrent (+-/01A2O+('olderName];

    Name'ile = [Path'older (+readme.t)t(];

    Namee)cel = [Path'older (+ho3a.)ls(];

    % operaciones del pro4rama

    ) = 5pi!.!pi;

    = sin#n6)$;

    % 4rafcar

    sin = f4ure;

    set#sin,(Name(,['olderName (7sin(],(Number1itle(,(o8($;

    plot#),$;

    4ridon;

    title#(sin#)$($

    le4end#( = sin#)$($;

    )label#()($; label#(sin#)$($;

    % crear las carpetas para 4uardar los resultados

    m9dir#[PathCurrent (+-/01A2O(], 'olderName$;

    % crear un archi&o .t)t con datos

    fle?2 = open#Name'ile,(>($;

    print#fle?2,(Fn%Gs %*sFnFn(,()(,(n6sin#)$($;

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clc.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/all.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/tic.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/tic.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/sin.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/grid.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/title.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/legend.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/ylabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/mkdir.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/clc.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/all.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/tic.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/pi.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/sin.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/grid.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/title.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/legend.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/ylabel.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/mkdir.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html
  • 7/24/2019 MATLAB Manipulacion de Archivos Excel

    4/6

    print#fle?2,(%G.* %*.HFn(,),$;

    print#fle?2, (FnFn1iempo imulaci@n acumulado! % se4undosFn(,toc$;

    close#fle?2$;

    % 4uardar f4ura en la carpeta

    sa&eas#sin, [Path'older (+sin.f4(], (f4($;

    % 4uardar >or9space en la carpeta

    sa&e#[Path'older (+>or9space.mat(]$;

    sa&e#[Path'older (+sin.mat(], (($;

    %:uardar datos en archi&o -C-0

    titulo = rite#Namee)cel, titulo, , (A($;

    )ls>rite#Namee)cel, datos, , (A*($;

    end

    -)plicaci@n!

    0a manera Iue se ha utiliJado en este caso es el traba3ar con un path

    absoluto, para ello, en las lKneas " L se obtiene el Pathde la Current

    Folder a tra&Ms de fleattrib!

    "L

    [stat,struc]= fleattrib;ParhCurrent = struc.Name;

    2onde struc.Namealo3a un dato similar aC!F/sersF))))))F2ocumentsFA10A , o

    el Path en el Iue te encuentres traba3ando.

    -n realidad confleattrib#(name($se puede obtener modifcar los atributos

    paraname, donde namees un path absoluto o relati&o de una carpeta o

    archi&o.

    -n las lKneas H se crea el nombre path de las carpetas # experiencia$

    de orma dinmica en relaci@n a la &ariable ndel for. 0a

    unci@nnum*str#n$ con&ierte nQmero a strin4. ecordar Iue las &ariables

    creadas #FolderName PathFolder$ son de tipo char. -n la lKnea * * se

    crean los nombres para los archi&os .txt .xls.

    A continuaci@n se declarar unas &ariablesxy, Iue para el caso no tienen

    importancia, su unci@n es la de ser&ir de e3emplo de datos para

    almacenar 4rafcar.

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/toc.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/saveas.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/toc.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/saveas.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.html
  • 7/24/2019 MATLAB Manipulacion de Archivos Excel

    5/6

    -n las lKneas *R a la "", se realiJa una 4rfca, i4ualmente como e3emplo,

    prestar atenci@n a la lKnea *H donde se asi4na la f4ura a crear en la

    &ariable fsinal4o importante para poder 4uardarla lue4o, a continuaci@n

    en la lKnea * se setea el nombre de la f4ura.

    -n la lKnea "R creamos las carpetas con el comando comQn para O, mkdir.

    "R m9dir#[PathCurrent (+-/01A2O(], 'olderName$;

    -n las lKneas L a la LL, se crea el archi&o .t)t. Primero se crea un archi&o

    con el nombre NameFile se le asi4nan permisos de escritura # w$. 0ue4o

    se escribe los datos, en la lKnea L los tKtulos de la tabla, \nson saltos de

    lKnea %6s se refere a datos G caracteres strin4 #para maor idea del

    asunto puedes re&isar la auda del comandoprinten atlab$. -n la lKnea

    L* se a4re4an los datos. 'inalmente se debe cerrar el archi&o conclose.

    L

    L*

    print#fle?2,(Fn%Gs %*sFnFn(,()(,(n6sin#)$($;

    print#fle?2,(%G.* %*.HFn(,),$;

    Figura 2. readme.txt

    Para 4uardar una f4ura, se utiliJa el comando sa&eas, como en la lKnea LR,

    a4re4ando un path, el nombre ormato.

    Para 4uardar las &ariables del workspacese emplea el comando sa&e, se

    puede 4uardar todas las &ariables como en el e3emplo o especifcar una o

    &arias de ellas como el la lKnea si4uiente donde se 4uarda la &ariabley.

    'inalmente en las lKneas EL a la ER se 4uarda inormaci@n en un archi&o de

    e)cel, para lo cual se emplea el comando )ls>rite, se introduce un tKtulo en

    la fla #celdas A de e)cel$ los datos a partir de la celda A* *.

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mkdir.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/saveas.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/mkdir.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/saveas.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.html
  • 7/24/2019 MATLAB Manipulacion de Archivos Excel

    6/6

    Figura 3.%o&a de Exce$