使用fprintf时无效的文件标识符错误

时间:2016-04-21 18:28:40

标签: matlab error-handling identifier

这是我的代码。

fileID=fopen(filename,'w');
fprintf(fileID,'Filename: %s\n',filename);
fprintf(fileID,'   Date: %s\n',Date);

我稍后在代码中关闭该文件。如果我输入文件名,它将在文件中写入文件名以及日期。但是,稍后我尝试将用户选择的用户名写入.txt文件

clearvars username
fprintf(2,'Any previous username will be overwritten\n');
fprintf('Input user name:');
username=input('','s');
fprintf(fileID,'Username: %s',username);

我收到一个错误,说我有一个无效的文件标识符,并使用fopen创建一个vlid文件标识符,我不知道我的代码有什么问题。我在使用fopen时尝试了不同的权限,并且我确保我打开的文件在我的目录中。

    case '1'

        clearvars filename 
        fprintf(2,'Any previous filename has been cleared\n');
        fprintf('Input your chosen filename:');
        filename=input('','s');
        infinitypermission=true;

        while infinitypermission

            fprintf('How would you like the data in the file, if any, to be treated?\n');
            fprintf('1) Override data in file, if any\n');
            fprintf('2) Append data in file, if any, to the end\n');
            fprintf('Enter your choice:');
            permission=input('','s');
            Date=date;
            infinitypermission=true;

            switch permission

                case '1'

                    fileID=fopen(filename,'w');
                    fprintf(fileID,'Filename: %s\n',filename);
                    fprintf(fileID,'   Date: %s\n',Date);
                    infinitypermission=false;

                case '2'

                    fileID=fopen(filename,'a');
                    fprintf(fileID,'Filename: %s\n',filename);
                    fprintf(fileID,'  Date: %s\n',Date);
                    infinitypermission=false;

                otherwise

                    fprintf(2,'Error: Input must be 1 or 2\n');
            end
        end

    case '2'

        %clears any data that has been previously loaded
        clearvars Data Data2 Data_file Mean Median Mode Var Stdev Min Max Count filetrue
        fprintf(2,'Any previously loaded data has been cleared\n');
        fprintf('Enter the data you would like to load: ');
        Data_file=input('','s');
        %checks  to see if the file is in the directory
        filetrue=exist(Data_file,'file');


        if filetrue==2
            Data=importdata(Data_file);
            numeric=isnumeric(Data);
            fprintf('%s loaded\n', Data_file)

            infinity8=true;

            while infinity8

                fprintf('Would you like the analyze the data in total or analyze by column?\n');
                fprintf('1) In total\n');
                fprintf('2) By column\n');
                fprintf('Enter your choice:');
                choice4=input('','s');

                switch choice4

                    case '1'

                        Data=vertcat(Data(:,1),Data(:,2));
                        Mean=mean(Data);
                        Median=median(Data);
                        Mode=mode(Data);
                        Var=var(Data);
                        Stdev=std(Data);
                        Min=min(Data);
                        Max=max(Data);
                        Count=length(Data);
                        infinity8=false;

                    case '2'

                        Mean=mean(Data);
                        Median=median(Data);
                        Mode=mode(Data);
                        Var=var(Data);
                        Stdev=std(Data);
                        Min=min(Data);
                        Max=max(Data);
                        Count=length(Data);
                        infinity8=false;

                    otherwise
                        fprintf(2,'Error: Must enter 1 or 2\n');
                end
            end


            switch numeric %tests to see if the data is useable

                case '0'
                    fprintf(2,'Error: Data must be numeric .txt file \n');
                    clear
            end %end of switch
        else
            fprintf(2,'Error: File not found in directory\n');
        end


    case '3'
        %clears data and any variables that go with it
        clearvars Data Data2 Data_file
        fprintf(2,'Data cleared\n\n');

    case '4'

        clearvars username
        fprintf(2,'Any previous username will be overwritten\n');
        fprintf('Input user name:');
        username=input('','s');
        fprintf(fileID,'Username: %s',username);

0 个答案:

没有答案
相关问题