필터 지우기
필터 지우기

a way to use data from a function in another function without running whole program

조회 수: 2 (최근 30일)
I am working on a code that using K-Means and there are For loops . This code take hours to make results. These results I need to use it, But It will be improper for me to run the code from beginning every time. I need to know how can I save time, and a way to begin the code from the new functions that I add but it takes the results from old functions. I hope that my question is clear enough. Thanks in advance,

답변 (1개)

dpb
dpb 2015년 1월 16일
No magic is available, sorry. You'll have to save the results when they're computed and then write the function(s) to be able to take those results if available and use them.
See
doc save
doc load
for probably the simplest way to write/read variables without any more fuss than necessary.
  댓글 수: 4
eman
eman 2015년 1월 17일
I've splitted the code in to Two.m files the first file code is:
clc;
clear all;
close all;
fontSize = 20;
TiledIm=[];
TrnImgPath='G:\Master_Students\Iman\UIUC\PNGImages\TrainImages\';
blockSizeR=16;
blockSizeC=16;
K=70;
for i=1:100
rgbImage=[TrnImgPath 'pos-' num2str(i) '.PNG'];%Reading positive Pictures
rgbImage=imread(rgbImage);
rgbImage =im2double(rgbImage);
% Display image full screen.
% imshow(rgbImage)
tmpTile = im2col(rgbImage,[blockSizeR blockSizeC],'sliding');
tmpTile = tmpTile';
TiledIm=[TiledIm; tmpTile];
end
opts = statset('Display','iter','MaxIter',400);
[IDX,Centers] = kmeans(TiledIm,K,'distance','sqEuclidean','Options',opts);
%///////////////////////////////////////////////////////////////////
%show centroids plot
%for i=1:K
% subplot(8,7,i);
% imshow( reshape(Centers(i,:),[blockSizeR blockSizeC]));
%end
%///////////////////////////////////////////////////////////////////
%/********codebook*/
Codebook.Centers=Centers;
Codebook.K=K;
Codebook.blockSizeR=blockSizeR;
Codebook.blockSizeC=blockSizeC;
save Codebook.mat Codebook;
And the second file code is
clc;
clear all;
close all;
%%load codeboock Centers and parameters
load Codebook;
fontSize = 20;
TiledIm=[];
TrnImgPath='G:\Master_Students\Iman\UIUC\PNGImages\TrainImages\';
blockSizeR=16;
blockSizeC=16;
K=70;
%convert centroids to image
for i=1:K
CImg= col2im(Centers,[256 2125], 'sliding');
imshow( reshape(Centers(i,:),[blockSizeR blockSizeC]));
end
when i run the second file this error message appeared:
Undefined function or variable 'Centers'.
Error in ToLoad (line 16)
CImg= col2im(Centers,[256 2125], 'sliding');
per isakson
per isakson 2015년 1월 17일
편집: per isakson 2015년 1월 17일
preallocate TiledIm to its final size, it will save some time
&nbsp
Replace
Centers
by
Codebook.Centers
You saved a structure named Codebook

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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by