Clear Filters
Clear Filters

Pass text file as argument to function?

3 views (last 30 days)
Hi,
I have a homework assignment where we have a main function that calls 3 other functions: one reads a system of equations from a text file, one solves the system, and one writes the system and its solutions to a new text file. I'm supposed to pass the name of the text file as the argument to the main function (or have the user be able to do so) so that I don't have to modify the code at all to be able to read from different text files. Right now I have a script instead of a function for the main function, and everything works if you modify the name of the text file in the code. How do I change it so that I can call the function with the text file as the argument? Do you pass it as a string? How do I call the subfunctions within the main function with a text file name as an argument?
Thank you!

Accepted Answer

Image Analyst
Image Analyst on 25 Sep 2016
Let's say your main function is called main.m and it's a script right now. Simply put this line at the first line of that main.m file:
function main(fullFileName)
then inside the function call fgetl() or whatever you want. But then to pass the string into main, you're going to have to call uigetfile() either from the command line, or from another script or function.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!