How to save .mat file in a function?

Hello, i have a function, i wanna save the result to .mat file. i try to execute save example.mat result , but i doesn't work. what should i do? this is my function
function result=plus(images)
[rmax, cmax] = size(images);
for row = 1:rmax;
for col = 1:cmax;
if images(row,col)== 1
a=14;
b=2;
result=a+b
end
end
end
save example.mat result
I purposely don't add a semicolon in the "result = a + b", in order to show the row and column which produces number 16
thanks in advance

답변 (2개)

Carlos
Carlos 2013년 4월 3일

0 개 추천

Your last line should be
save('example.mat', 'result');

댓글 수: 3

tedy
tedy 2013년 4월 3일
Thank you for your reply, but i think it's still the same with my code. i mean that, when you run the code above, it results 16,16,16,16...so on. But, why the example.mat only have matrix 1x1, it's doesn't has the same matrix with the result which is 16,16,16...
Matt Kindig
Matt Kindig 2013년 4월 3일
It's unclear what you expect 'result' to be. You define 'result' to be a+b, which will always give you 16 since you have defined a=14 and b=2. In what sense does your code not work?
tedy
tedy 2013년 4월 3일
i'm not focus on the value of result. it's free to be what you want 16,12,3,5.., and so does the a&b value, it's free to be what you want 1,3,4.. i just wondering, why example.mat only has matrix 1x1, whereas the results has so many number

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

Walter Roberson
Walter Roberson 2013년 4월 3일

0 개 추천

You are writing over all of the variable "result" on every iteration of the loop. Try
result(row, col) = a+b;

댓글 수: 1

tedy
tedy 2013년 4월 4일
Thank you for your reply, but it takes all the row and col. How to display only the calculation of a+b and also the matrix contain only the calculation? for example the results are 16,16,16,16.. so, the matrix also only contain 16,16,16,16.. not all the row and col

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2013년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by