kreutz-lab / Rcall

R function interface for Matlab
BSD 3-Clause "New" or "Revised" License
23 stars 4 forks source link

OPENR in Rpush.mat not being read correctly #7

Closed ghcorrigan closed 2 years ago

ghcorrigan commented 2 years ago

Hi, When I tried to use Rcall, I got an error "all elements of a list must be named" and this was being generated by the line attach(data_Rpush) this is because the structure OPENR in Rpush.mat was not read in correctly. I don't know what the issue is with R.matlab 's readMat is, because I can see where you have specifically defined the file version a v7 to work with structures, but it just does not appear to work for me. Specifically, the nested structures in OPENR appear as unnamed variables in the list. My work around so far has been to prevent Rpush.mat from being initialized with OPENR, and instead just saved a dummy variable, and did not use structures. I checked the filetype created by save('Rpush.mat','OPENR','-v7') and it said 5.0 MATFILE on two different computers, but I don't know if that is meaningful. If there is any suggestion on how to make this work without my janky work around, please let me know. Thanks for this helpful tool!

JanineEgert commented 2 years ago

Hi ghcorrigan, thanks for using Rcall. Could you give me some more detail so I can reproduce your issue? Did you test if Rcall and R.matlab work correctly by pushing just an integer or a test structure variable? How does your structure look like? May you give an example structure when the error occurs so I can have a look into it. Which Matlab/R versions do you use? Best, Janine

ghcorrigan commented 2 years ago

The issue for me is that R.matlab's readMat function does not appear to be reading structures correctly. if I run test = struct("a",{1,2,3},"b",[4 5 6],'c',"cows") save("test.mat","test","-v7") in Matlab, and then library("R.mat;ab") testR<-readMat("test.mat") testR is a 'List of 5' with one item called 'test' which is a list [3x1x3] of length 9. The next entry is unnamed '{{2}}' that contains [4 5 6], then an entry 'MCOS' that is 6 raw hex values, and two more unnamed entries [[4]] = integer [6x1] and [[5]] = integer [1 x 1232]. I am running Matlab 2020a and R version 4.2.1. I checked the R.matlab github and others appear to have similar issues, but it is not clear why, and support seems to have stopped.

In summary, R.matlab does not correctly open matlab structures for me. For anyone that is having this issue, it does not appear that the structure OPENR saved in Rinit.m needs to be a structure, and initializing it with a simple vector variable allows this issue to be sidestepped.

JanineEgert commented 2 years ago

Dear ghcorrigan, using your given struct variable, indeed the above named error appeared. However, if you change your character variable to a string, the R.matlab package works as it should. So just make sure to convert your elements beforehand. In the given example, this would be: test = struct("a",{1,2,3},"b",[4 5 6],'c','cows'). And it works nice and need.

Have fun with the tool. Best, Janine

ghcorrigan commented 2 years ago

Thanks for pointing that out! Technically, it is the inverse, my string variable needed to be a character variable. I say this because it is important to define the path and libpath in Rinit.m as 'character vectors', and not "strings", otherwise when they get passed to the OPENR structure, they stay as strings, and readMat cannot read the OPENR structure correctly. This was what caused my problems initially. Thank you again for your help! Cheers Ben

JanineEgert commented 2 years ago

Hi Ben, thanks for pointing that out. The path variables in Rinit now also accept string input and internally convert "string" variables into 'character'. Best, Janine