Pass text file as argument to function?

3 次查看(过去 30 天)
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!

采纳的回答

Image Analyst
Image Analyst 2016-9-25
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by