1
votes

I have this piece of MATLAB code:

path(path,'./Classes');
locationID = 'Z8Ksof1rzm';
poolobj = gcp('nocreate');
if isempty(poolobj)
    parpool(4)
else
    %do nothing. we already have parpool running
end
myFiles = dir(strcat('./exportParse/exportLocation_', locationID));
MachineData = cell(length(myFiles)-2,1);
disp(myFiles)
parfor iFile =3:length(myFiles)
        jsonFile = myFiles(iFile).name;           
        MachineData{iFile-2} = loadjson(strcat('./exportParse/exportLocation_', locationID,'/',jsonFile));
end

The script runs pretty well from the MATLAB desktop. I see no errors and I get all 4 processors working on the json parsing. At the end, I get MachineData filled with the desired information. All good.

The problem occurs when I call

matlabPath="/Applications/MATLAB_R2014b.app/bin/matlab -nodesktop -nosplash -r"
$matlabPath "run myScript.m"

where myScript.m is the file containing the code above. The script won't run again, it says this:

10x1 struct array with fields:

    name
    date
    bytes
    isdir
    datenum

Error using readDataAprocam (line 17)
Improper index matrix reference.

Error in run (line 63)
evalin('caller', [script ';']);

as you can see, the line dis(myFiles) returns a valid struct array of files.

Line 17 is the line of the parfor command.

Note that on the shell and in MATLAB I'm located on the same path. I also shut down the parpool on the matlab desktop so the script running from the shell can claim it. there is no problem here as well.

What does Improper index matrix reference means on this context? Why does it run from the matlab desktop and not from the shell?

I'm running Mac OS X 10.11.3 and MATLAB 2014b.

1
Can you try it on the shell using a normal for loop? I don't expect it to solve the issue, but it typically returns better error messages. - Daniel
What do You have on line 63? Maybe You can access A(1,2) element by A(3) (for A with size 2XN) in Matlab desktop but not from shell. - Crowley
I've made some changes in the code, and now I'm getting another error. It seems that I solved this one but got sucked further. I'm going to post my "answer here" and make a new question with the next problem I'm facing. Btw @Daniel my new code runs with for, but not with parfor - otmezger
@Crowley I think this error might be related to the function run. My script doesn't have more than 20 lines. - otmezger
@otmezger Ah... I can see it now. Then one must find the path to the run() command. Or You can nest it into try catch ME envelope and display the error message in ME. - Crowley

1 Answers

0
votes

I'm not sure what I did, but I got rid of this error. My code looks like this now:

myScript.m:

%% settings
path(path,'./Classes');
locationID = 'Z8Ksof1rzm';
poolobj = gcp('nocreate');
if isempty(poolobj)
    parpool(4)
else
    %do nothing. we already have parpool running
end
%% get files
disp('ok. lets try it')
myFiles = dir(strcat('./exportParse/exportLocation_', locationID,'/export*'));
MachineData = cell(length(myFiles),1);

parfor iFile =1:length(myFiles)
    data = importJSONFile(iFile,locationID,myFiles);
    MachineData{iFile} =data;
end %looping over files
MachineData

Script importJSONFile:

function data = importJSONFile(fileIndex,locationID,myFiles)
  jsonFile = myFiles(fileIndex).name;
  filePath = strcat('./exportParse/exportLocation_', locationID,'/',jsonFile);
  data = loadjson(filePath);
  %data = struct;
end

The code runs from the shell ONLY if I change the parfor to for. if I leave the parfor it will trow the error

Error using myScript (line 16)
Conversion to char from cell is not possible.

Error in run (line 63)
evalin('caller', [script ';']);

Line 16 is the parfor line.

... but, I got rid of the error above.