我有这段MATLAB代码:
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
该脚本在 MATLAB 桌面上运行得很好。我没有看到任何错误,我让所有 4 个处理器都在进行 json 解析。最后,我MachineData
充满了所需的信息。都很好。
当我打电话时出现问题
matlabPath="/Applications/MATLAB_R2014b.app/bin/matlab -nodesktop -nosplash -r"
$matlabPath "run myScript.m"
其中myScript.m
是包含上述代码的文件。脚本不会再次运行,它说:
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 ';']);
如您所见,dis(myFiles)
行返回一个有效的文件结构数组。
第 17 行是 parfor
命令的行。
请注意,在外壳和 MATLAB 中,我位于相同的路径上。我还关闭了 matlab 桌面上的 parpool,以便从 shell 运行的脚本可以声明它。这里也没有问题。
在这种情况下,Improper index matrix reference
意味着什么?为什么它从 matlab 桌面而不是从 shell 运行?
我运行的是 Mac OS X 10.11.3 和 MATLAB 2014b。
我不确定我做了什么,但我摆脱了这个错误。我的代码现在看起来像这样:
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
脚本导入 JSONFile:
function data = importJSONFile(fileIndex,locationID,myFiles)
jsonFile = myFiles(fileIndex).name;
filePath = strcat('./exportParse/exportLocation_', locationID,'/',jsonFile);
data = loadjson(filePath);
%data = struct;
end
仅当我将parfor
更改为 for
时,代码才会从 shell 运行。 如果我离开parfor
Error using myScript (line 16)
Conversion to char from cell is not possible.
Error in run (line 63)
evalin('caller', [script ';']);
16号线是parfor
线。
。但是,我摆脱了上面的错误。