how to call the function from the other file?

54 views (last 30 days)
Hi,
I have a function file attached, and the file I want to read with that function. The file I am trying to read does not have any extension as it is a binary file but the name of the file is 202209201120
How would I call that function ?
sofar I have tried but I think its wrong
close all; clear all; clc;
filename = '202209201120';
S = fileread(filename);
[int_gen_hd,rl_gen_hd,rl_datsp_hd,char_hd,int_datsp_hd,rr_dat_mat] = rdnim(S)
Error:
Error: File: rdnim.m Line: 3 Column: 79
Local function name must be different from the script name.
Error in raindata (line 4)
[int_gen_hd,rl_gen_hd,rl_datsp_hd,char_hd,int_datsp_hd,rr_dat_mat] = rdnim(S)

Accepted Answer

Walter Roberson
Walter Roberson on 7 Nov 2022
You editted the file rdnim and added the lines
close all; clear all; clc;
to the top of it. That turned rdnim from a function into a script: function files have the word function as the first non-comment word, and script files have anything other than "function" or "classdef" as the first non-comment word.
When you create a script file, it is valid to define functions at the end of the script (provided that the function has a matching end statement), with the exception that the function name defined cannot be the same as the script file. If MATLAB permitted defining a function with the same name as the script, then if the code gave the name, it would not be possible to determine whether the intent of the code was to invoke the script or to invoke the function of the same name inside the script.
When you include clear all inside code, the effect is to clear nearly all variables except independent handle objects (such as graphics objects). global variables are cleared, persistent variables are cleared, all variables accessible in the current function are cleared (including named parameters for the function.) I like to compare clear all to what happens when Wile E. Coyote blows up the bridge that he is standing on. If you have clear all inside your code, you should assume that the code will break.
Because of the clear all if you did manage to invoke the function stored inside the (now script) file, any parameters you might have passed would be cleared.
The line of code that you added has made the (now script) file useless.
  1 Comment
muhammad choudhry
muhammad choudhry on 7 Nov 2022
yea man realized! made a mess of simple work.... but its good now, start getting numbers. I will bother again once start analysing the numbers....

Sign in to comment.

More Answers (1)

Voss
Voss on 7 Nov 2022
Edited: Voss on 7 Nov 2022
Remove the first line from rdnim.m:
close all; clear all; clc;
so that rdnim.m starts with the function keyword:
function [int_gen_hd,rl_gen_hd,rl_datsp_hd,char_hd,int_datsp_hd,rr_dat_mat] = rdnim(fl_name)
(Modified rdnim.m attached.)
Then call rdnim with a file name as the argument, specifying the full path to the file:
filename = 'C:\some\directory\202209201120';
[int_gen_hd,rl_gen_hd,rl_datsp_hd,char_hd,int_datsp_hd,rr_dat_mat] = rdnim(filename)

Tags

Community Treasure Hunt

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

Start Hunting!