Macros in Matlab - repeating simple instructions

Hi there,
I have read on some websites that writing macros for Matlab is not advisable? I am trying to write several as I am about to be embarking on the same process for a number of different data sets. twenty five to be exact. I would like to do simple things like write macro to call my data set put it through two or three functions that I have written and then save the results in a particular place. Is that possible? Is there any good literature I can get on the topic?

2 件のコメント

Bran
Bran 2014 年 2 月 18 日
Also I was wondering if there was a way to get the script to run operations in batches for example I need to use my fucntion on various files and run it through the night so I want to write a macro that can tell me prgram to switch files after the results have been obtained.
Iain
Iain 2014 年 2 月 18 日
It kinda depends what you mean by "macro". VB macros can be functions or subs, but you can access them like shortcuts.

サインインしてコメントする。

 採用された回答

Iain
Iain 2014 年 2 月 18 日

1 投票

Step 1: Write a "function" that takes as input, an argument for filename in, and filename out, opens & reads your input file, does the operations to it, and saves the output in the desired output filename.
Step 2: Write a script that feeds the appropriate input & output filenames to that function.
Step 3: Run the script.
Eg, file1, called "fred.m"
function fred(in,out)
x = load(in); % or importdata or csvread or xlsread or whatever your reading function is.
y = sin(x);
save(out,'y')
file 2: called "process_it_all"
in = {'file1.mat','file2.mat'};
out = {'wibble','wobble'};
for i = 1:numel(in)
fred(in{i},out{i})
end

1 件のコメント

Bran
Bran 2014 年 3 月 5 日
Hi Iain,
So sorry to add this comment so late. I was wondering if you could help. your suggestion has been throwing up errors for me. I thought a function should be defined as [out] = function (in) Also what if I want the function to do two things, should that then be [out,out2] = function(in,in2) also it keep telling me that load should be loading up a string. It doesn't seem to be able to recognize that in already has strings. Any ideas?
many thanks in advance

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2014 年 2 月 18 日

0 投票

For something like that you would likely be better off writing functions.
But yes, you can write scripts. scripts run in the context of the current workspace.

1 件のコメント

covariant_cat
covariant_cat 2018 年 6 月 6 日
How do you deal with import? Neither another function nor another script can import packages for a function.

サインインしてコメントする。

Bran
Bran 2014 年 2 月 19 日
編集済み: Bran 2014 年 2 月 19 日

0 投票

Hi guys
Thanks so much for the help I also wanted to check something say for example I wanted to plot a graph of y = sin(x) for example and save it to a particular folder ofor each loop how would I go about this?
Would something like this work?
baseFileName = sprintf(out2,k);
% Specify some particular, specific folder:
fullFileName = fullfile('D:\myPlots', baseFileName);
figure(1); % Activate the figure again.
export_fig(fullFileName); % Using export_fig instead of saveas.

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2014 年 2 月 18 日

コメント済み:

2018 年 6 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by