Calling MATLAB functions in Visual Basic

I made a very simple program to try how to call matlab functions in visual basic. My matlab program is this:
function showRGB()
origImage = imread('imagefromvisualbasic.jpg');
redChannel = origImage(:,:,1);
greenChannel = origImage(:,:,2);
blueChannel = origImage(:,:,3);
end
Here's the GUI of the app made in vb.
I've tried reading this help and here's the code of the vb.
1. How will I use the imread in matlab to read the image from the openDialogBox of VB?
2. How will I use access the output from MATLAB to show the RGB Channels in the picture box?
Thank you

 채택된 답변

Friedrich
Friedrich 2014년 1월 16일
편집: Friedrich 2014년 1월 16일

0 개 추천

Hi,
  1. you can't. You would need to send over the RGB image data from VB to MATLAB, e.g. by using the PutWorkspaceData method
  2. the way you have desgined your function you can't. You would need to make the RGB channels output variables
function [redChannel ,greenChannel,redChannel] = showRGB()
and then call the Execute method
ml.Execute("[a,b,c] = showRGB()")
to create the variables a, b and c in the base workspace which you then can get to VB by calling GetWorkspaceData. The example you mentioned does all that.

댓글 수: 6

Thank you very much for the help. I tried fixing my codes but I got an error when running it. here's the new matlab code:
function [redChannel,greenChannel,blueChannel] = showRGB(inputImage)
RGBImage = imread(inputImage);
redChannel = RGBImage(:,:,1);
greenChannel = RGBImage(:,:,2);
blueChannel = RGBImage(:,:,3);
end
Here's the VB program:
Thanks again for the help. Sorry, but I'm not that good in visual basic.
Hi,
i doubt that the conversion from tghe VB type Image to something MATLAB can use is done automatically. YOu need to convert that by your self. Basically the data converion rules from here
web([docroot '/matlab/matlab_external/handling-com-data-in-matlab-software.html'])
apply. BTW: what does the MATLAB side receive when your try to put the Image data type to the MATLAB workspace?
Elvin
Elvin 2014년 1월 20일
I'm still confused with this one. Is there any easier method to do this?
By the way, when I run the program using the upload button, nothing is happening in the matlab. No variable was sent to the workspace. But the matlab command windows is popping up.
By the way, is there a way to compile my matlab code so I can use it in visual basic in such a way that a pc without matlab can still run the program?
Friedrich
Friedrich 2014년 1월 20일
I doubt there is an easier way.
You could compile your MATLAB Code with MATLAB Builder NE in order to generate a COM component out of it. You could then use that back in VB. However this wouldn't solve the Image datatype conversion. You would still need to do this on your own.
Elvin
Elvin 2014년 1월 20일
Can you show me a sample? I'm really confused with this one.
Friedrich
Friedrich 2014년 1월 20일
I am not really VB programmer but I guess you have to call the save method and save the image into a stream. As a stream use some byte array. I guess googling for that task should give some hints.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

질문:

2014년 1월 16일

댓글:

2014년 1월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by