Reply to topic  [ 3 posts ] 
Working on a pdb2mat, having trouble restoring variables 
Author Message
Yorick Padawan

Joined: Tue Jun 07, 2011 1:46 pm
Posts: 32
Location: UNH
Post Working on a pdb2mat, having trouble restoring variables
Hi,

I want to dynamically restore variables and be able to save each variable and its data to a .mat file using ml4. The ml4 part works fine but my question is how do I get each variable name and be able to restore each one without hardcoding exactly what variables I need? Would it be possible to use pointers and get_vars()?

I found on a different post how to get the variable names in a string format which solves one part of the problem.

This is my code so far:
Code:
func pdb2mat(pdbFile)
{
   f = openb(pdbFile);
   restore, r, x, y;
   // gets a list of the variable names in string form
   list = *(get_vars(f)(1));

   // delete extension on pdbFile and replace with .mat or .m
   
   close, f
   /*
   matFile = swrite("%s.mat", pdbFile);
   ml4write(matFile, r, "r", "w");
   ml4write(matFile, x, "x", "a");
   ml4write(matFile, y, "y", "a");
   n = numberof(list);
   for (i = 1; i <= n; i++)
   {
      ml4write(matFile, varname, list(i), "a");
   }
   */
}


For now I'm hard coding it but it would be much easier to be able to apply this function to any pdb file.

Edit: If this is not possible to do, then I'll also write a program in matlab that can read the binary .pdb files.

Thank you,
Ronak


Wed Jul 20, 2011 4:52 am
Profile
Yorick Master

Joined: Wed Jun 01, 2005 11:34 am
Posts: 112
Post Re: Working on a pdb2mat, having trouble restoring variables
I believe the missing piece to the puzzle for you is the "get_member" function. I am not familiar with matlab or ml4write, but I adjusted your code to what I think might accomplish what you're looking for (untested though).

As an aside, in Yorick, functions are called as "x = foo(bar)" while subroutines are called as "foo, bar". If you have a line that says "foo(bar)", you'll get the return value output to your screen which may or may not be what you wanted.

Code:
func pdb2mat(pdbFile)
{
   f = openb(pdbFile);
   // gets a list of the variable names in string form
   list = *(get_vars(f)(1));

   // delete extension on pdbFile and replace with .mat or .m
   matFile = swrite("%s.mat", pdbFile);

   n = numberof(list);
   for (i = 1; i <= n; i++)
   {
      ml4write, matFile, (get_member(f, list(i))), list(i), "w";
   }
   close, f
}


Wed Jul 20, 2011 8:05 am
Profile
Yorick Padawan

Joined: Tue Jun 07, 2011 1:46 pm
Posts: 32
Location: UNH
Post Re: Working on a pdb2mat, having trouble restoring variables
Wow thank you! That worked.

I updated the code a little because the format= was missing, "a" for append, and I also forgot to close the matlab file.

Code:
func pdb2mat(pdbFile)
/* DOCUMENT pdb2mat(pdbFile)
   Converts pdb yorick binary files to version 4 matlab files.
   prbFile = file name (string)

   ml4 plugin required
   ml4 supports up to 2D arrays
*/
{
   f = openb(pdbFile);
   
   // delete extension on pdbFile and replace with .mat or .m
   sel = strfind(".pdb", pdbFile);
   matFile = streplace(pdbFile, sel, ".mat");

   // gets a list of the variable names in string form
   list = get_members(f);
   n = numberof(list);
   for (i = 1; i <= n; i++)
   {
      ml4write, matFile, (get_member(f, list(i))), list(i), "a";
   }
   close, f
   ml4close, matFile;
}


This code will definitely provided a good reference. Thanks again.

Ronak


Wed Jul 20, 2011 10:50 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.