通过Python在MATLAB中保存.mat文件
这段来自 demo.m
的代码在我直接运行 demo.m
时在MatLab中工作得很好:
save('C:\PATH_TO_MY_FOLDER\boxes.mat', 'boxes');
所以当我查看文件夹时,确实发现了一个叫 boxes.mat
的文件被创建了,它里面存储了一个二维数组。
但是,当我从Python运行 demo.m
时(在下面的代码中),Matlab似乎打开了,但 boxes.mat
似乎没有被保存。
import win32com.client
h = win32com.client.Dispatch('matlab.application')
h.Execute ("C:\\PATH_TO_MY_FOLDER\\demo.m")
我是不是根本没有成功运行 demo.m
呢?
我在 demo.m
中添加了这一行
fprintf('hello');
在保存 boxes.mat
文件的那一行之前,但也没有得到那个输出。
我该如何确认 demo.m
是否真的从Python中执行了呢?我确实看到任务栏里有一个Matlab的按钮闪了一下。
这个 demo.m
的代码稍作修改,来源于 这里:
% This demo shows how to use the software described in our IJCV paper:
% Selective Search for Object Recognition,
% J.R.R. Uijlings, K.E.A. van de Sande, T. Gevers, A.W.M. Smeulders, IJCV 2013
%%
addpath('Dependencies');
fprintf('Demo of how to run the code for:\n');
fprintf(' J. Uijlings, K. van de Sande, T. Gevers, A. Smeulders\n');
fprintf(' Segmentation as Selective Search for Object Recognition\n');
fprintf(' IJCV 2013\n\n');
% Compile anisotropic gaussian filter
if(~exist('anigauss'))
fprintf('Compiling the anisotropic gauss filtering of:\n');
fprintf(' J. Geusebroek, A. Smeulders, and J. van de Weijer\n');
fprintf(' Fast anisotropic gauss filtering\n');
fprintf(' IEEE Transactions on Image Processing, 2003\n');
fprintf('Source code/Project page:\n');
fprintf(' http://staff.science.uva.nl/~mark/downloads.html#anigauss\n\n');
mex Dependencies/anigaussm/anigauss_mex.c Dependencies/anigaussm/anigauss.c -output anigauss
end
if(~exist('mexCountWordsIndex'))
mex Dependencies/mexCountWordsIndex.cpp
end
% Compile the code of Felzenszwalb and Huttenlocher, IJCV 2004.
if(~exist('mexFelzenSegmentIndex'))
fprintf('Compiling the segmentation algorithm of:\n');
fprintf(' P. Felzenszwalb and D. Huttenlocher\n');
fprintf(' Efficient Graph-Based Image Segmentation\n');
fprintf(' International Journal of Computer Vision, 2004\n');
fprintf('Source code/Project page:\n');
fprintf(' http://www.cs.brown.edu/~pff/segment/\n');
fprintf('Note: A small Matlab wrapper was made.\n');
% fprintf('
mex Dependencies/FelzenSegment/mexFelzenSegmentIndex.cpp -output mexFelzenSegmentIndex;
end
%%
% Parameters. Note that this controls the number of hierarchical
% segmentations which are combined.
colorTypes = {'Hsv', 'Lab', 'RGI', 'H', 'Intensity'};
colorType = colorTypes{1}; % Single color space for demo
% Here you specify which similarity functions to use in merging
simFunctionHandles = {@SSSimColourTextureSizeFillOrig, @SSSimTextureSizeFill, @SSSimBoxFillOrig, @SSSimSize};
simFunctionHandles = simFunctionHandles(1:2); % Two different merging strategies
% Thresholds for the Felzenszwalb and Huttenlocher segmentation algorithm.
% Note that by default, we set minSize = k, and sigma = 0.8.
k = 200; % controls size of segments of initial segmentation.
minSize = k;
sigma = 0.8;
% As an example, use a single image
images = {'000016.jpg'};
%images = {'C:\Users\Public\Pictures\Sample Pictures\Desert.jpg'};
im = imread(images{1});
% Perform Selective Search
[boxes blobIndIm blobBoxes hierarchy] = Image2HierarchicalGrouping(im, sigma, k, minSize, colorType, simFunctionHandles);
boxes = BoxRemoveDuplicates(boxes);
save('C:\work_asaaki\code\SelectiveSearchCodeIJCV\boxes.mat', 'boxes');
1 个回答
1
你在让Matlab执行命令'C:\PATH_TO_MY_FOLDER\demo.m',但这个命令并不是有效的Matlab命令。如果你把它输入到Matlab的控制台里,你会看到同样的错误信息。
你可能想要的是:
h.Execute ("cd('C:\\PATH_TO_MY_FOLDER');")
h.Execute ("demo;")
在Matlab中,你应该先切换到正确的文件夹,然后再运行这个命令。你也可以用run
这个命令把它写成一行,但因为你脚本里的相对路径,这样做可能会不成功。