X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 25 May 2007 12:43 PM by  anon
Making .sav files useful with other languages
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
25 May 2007 12:43 PM
    Hello, I have two structures saved in an IDL .sav file. Do you guys know how I can import this data to MATLAB. When I try, it says that .sav file is a proprietary format. I have a few IDL procedures that play with the data, and I'd like to port them to MATLAB. If it's not possible, can you suggest how I should go about saving these structures to a format that MATLAB would be able to read (preferably I would like the form of the structures preserved so that I won't have to think up new code/logic). Should I write to a file? Thank you, Sashank Karri

    Deleted User



    New Member


    Posts:
    New Member


    --
    25 May 2007 12:43 PM
    '.sav' is a proprietary file format for IDL. I do not think that there is any other image- or math-processing computer language that has developed a parser for these very IDL-specific files. Furthermore, structure datatypes, not just in IDL but in all computer languages, are programmer-specific datatypes, so, generally speaking, the programmer who creates a new structure datatype in one language also has to create a parser and definition for that structure datatype in each of the computer languages, with which he/she is sharing his/her structure. I am no expert in Matlab, but a quick look on Google makes me think that there are ways to implement a "struct importer" in Matlab that might be similar to the one that one might implement in IDL. This "struct importer" would be based on a simple exporter algorithm, which would have IDL writing out a struct definition in ASCII text using Matlab syntax for struct definition. That is, IDL would write out to a '.m' Matlab script file the Matlab code command for creating the struct. If the file ended up in the Matlab search path, then it could be executed any time afterward at the Matlab command prompt. I cannot promise that the below EXPORT_STRUCT_TO_MATLAB procedure offers exactly correct syntax for Matlab, but I suspect that I am at least pretty close on syntax. Matlab's 'struct( )' function seems to have, at least for "anonymous structures", a similar syntax to IDL's CREATE_STRUCT. I just don't know whether Matlab's 'struct ( )' can take as arguments nested function statements like IDL's CREATE_STRUCT can. Nevertheless, you can test my code with the following IDL commands IDL> file = filepath('rose.jpg', SUBDIR=['examples', 'data']) IDL> exampleStruct = file_info(file) IDL> ; Because FILE_INFO returns the struct as a 1-element array ... IDL> exampleStruct = exampleStruct[0] IDL> ; Check for the '.m' output file in your home directory ... IDL> export_struct_to_matlab, exampleStruct, 'exportedIDLstruct.m' My function below is not intended to be a robust all-purpose IDL struct exporter. It does not, for example, handle structure fields that might have array values or another nested structure. Those would require a more complex "parsing protocol", but even that more complex protocol might start with a variation on the procedure below. ; PRECONDITION: In this example function 'my_struct' fields must ; be ***scalar*** integers, floating point or string datatype only! PRO export_struct_to_matlab, my_struct, my_matlab_script_output_file tagNames = tag_names(my_struct) matlabCommand = "importedIDLstruct = struct(" for i = 0, n_elements(tagNames)-1 do begin matlabCommand += ("'" + tagNames[i] + "', ") currentDatatype = size(my_struct.(i), /TNAME) case currentDatatype of 'BYTE': value = 'uint8(' + strtrim(fix(my_struct.(i)), 2) + ')' 'INT': value = 'int16(' + strtrim(my_struct.(i), 2) + ')' 'LONG': value = 'int32(' + strtrim(my_struct.(i), 2) + ')' 'FLOAT': value = 'single(' + strtrim(my_struct.(i), 2) + ')' 'DOUBLE': value = string(my_struct.(i)) 'LONG64': value = 'int64(' + strtrim(my_struct.(i), 2) + ')' 'STRING': value = "'" + my_struct.(i) + "'" endcase matlabCommand += value if i ne (n_elements(tagNames)-1) then matlabCommand += ", " endfor matlabCommand += ')' print, matlabCommand ; for debugging openw, lun, my_matlab_script_output_file, /GET_LUN printf, lun, matlabCommand free_lun, lun END Hope this helps, James Jones
    You are not authorized to post a reply.