Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to open a file with a function?

Subject: How to open a file with a function?

From: Diego Zegarra

Date: 30 Jan, 2009 21:22:02

Message: 1 of 2

First how do I convert this into a function, where the text file labeled '6x3.txt' would be the input for the function and I dont need to get numMachines, numJobs, PT and ST. The .m file is called fgetmat.m, so how can I call this function from a different .m file where I want to input the filename.
**********************************************
data = txt2mat('6x3.txt',0);

% Get the # of machines and jobs from file
numMachines = data(1,1);
numJobs = data(2,1);

% Save Machines, Jobs to a matrix
A = [numMachines,numJobs];
nextRowIndex = 3;

% Saves the processing times to a matrix
PT = data(nextRowIndex+(1:numJobs), 1:numMachines);

% Saves the setup times to an array
ST = cell(numMachines,1);
for idx = 1:numMachines
    nextRowIndex = nextRowIndex + 1 + numJobs;
    ST{idx} = data(nextRowIndex+(1:numJobs), 1:numJobs);
end
**********************************************
So in another .m file I am calling it right now like this,

fgetmat;

which works but I want to be able to write the filename in this .m file. In this file where I want to write the filename, I use the values numMachines, numJobs, PT and ST.

Thank you for the time spent trying it!

Diego

Subject: How to open a file with a function?

From: someone

Date: 30 Jan, 2009 21:45:05

Message: 2 of 2

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <glvr1q$9cp$1@fred.mathworks.com>...
> First how do I convert this into a function, where the text file labeled '6x3.txt' would be the input for the function and I dont need to get numMachines, numJobs, PT and ST. The .m file is called fgetmat.m, so how can I call this function from a different .m file where I want to input the filename.
> **********************************************
> data = txt2mat('6x3.txt',0);
>
> % Get the # of machines and jobs from file
> numMachines = data(1,1);
> numJobs = data(2,1);
>
> % Save Machines, Jobs to a matrix
> A = [numMachines,numJobs];
> nextRowIndex = 3;
>
> % Saves the processing times to a matrix
> PT = data(nextRowIndex+(1:numJobs), 1:numMachines);
>
> % Saves the setup times to an array
> ST = cell(numMachines,1);
> for idx = 1:numMachines
> nextRowIndex = nextRowIndex + 1 + numJobs;
> ST{idx} = data(nextRowIndex+(1:numJobs), 1:numJobs);
> end
> **********************************************
> So in another .m file I am calling it right now like this,
>
> fgetmat;
>
> which works but I want to be able to write the filename in this .m file. In this file where I want to write the filename, I use the values numMachines, numJobs, PT and ST.
>
> Thank you for the time spent trying it!
>
> Diego

What DO you need to get from the function?

If I understand what you are asking, simply replace the line:
data = txt2mat('6x3.txt',0);
with:
function A = fgetmat(data)
and save it as fgetmat.m

Then form the command line (or in any other m-file) call it as:
A = fgetmat(data)
assumming you want A returned and data is defined in the command window or in the m-file as something like:
data = txt2mat('6x3.txt',0);

(I'm not sure what "txt2mat" is.)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us