小编Jac*_*b M的帖子

使用 fprintf() 和 disp() 函数在 MATLAB 中的命令窗口中显示消息?

目前正在开发一个项目,其中我必须接受多个用户输入。因为我的输入提示必须向用户概述有关如何输入值的特定格式,这使得每个输入提示相当冗长,因此我认为用换行符分隔每个输入提示是合适的,以便很容易区分它们/这样看起来就不错了。最后一个提示有两行长,因此如果它们都混杂在一起而不是用换行符分隔,则很难将其与其他提示区分开来。

fprintf()我已经探索了和的用法disp(),并发现它fprintf()有一些棘手的行为,有时如果不包含诸如 等之类的东西就无法工作fflushf()。此外,我读到它fprintf()实际上是用于将数据写入文本文件(来自 MathWorks 页面) ,至少),并且将其用于其他目的,如果确实有更简单的方法,我肯定会看到我的教授扣分(我们对脚本效率的评分非常严格)。

disp()命令似乎更符合我正在寻找的内容,但是我找不到任何能够支持\n. 目前,我已经采用了替换\nwith的用法disp(' '),但这肯定会导致扣分。

TL;DR有没有更有效的方法来创建换行符而不使用fprintf('text\n')?我将附上我的脚本的一部分供您查看:

disp('i) For the following, assume Cart 1 is on the left and Cart 3 is on the right.');
disp('ii) Assume positive velocities move to the right, while negative velocities move to the left.');
prompt = '\nEnter an array of three cart masses (kg) in the form ''[M1 M2 M3]'': '; …
Run Code Online (Sandbox Code Playgroud)

formatting matlab newline

3
推荐指数
1
解决办法
4019
查看次数

试图在输入(char数组)中间返回char给出"分段错误(核心转储)"?

简而言之,我必须能够在输入(char数组)的中间返回字符,作为我们第一个C赋值的一部分.但是,到目前为止,我的代码是返回"Segmentation fault(core dumped)".我稍微阅读了一下,并了解到基本上我可能正试图访问/修改"我无法使用"的数据,可以这么说.这是我的代码:

#include <stdio.h>
#include <string.h>

char input[30];
int inputLen;
char midChar;

int main()
{
    printf("Type in some text, and the press the Return/Enter key: ");
    fgets(input,sizeof(input),stdin);
    printf("\nYour input: %s",input);
    inputLen = strlen(input)-1;
    printf("Length of your input is %d characters.",inputLen);

    if((inputLen % 2) == 0) {
        midChar = input[(inputLen/2)+1];    // >>> PROBLEM HERE <<<
    }
    else {
        midChar = input[((inputLen+1)/2)+1];    // >>> PROBLEM HERE <<<
    }
    printf("%s",midChar);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这两条线>>> PROBLEM HERE <<<是我认为我已经缩小到成为问题根源的线.

请注意:我参加了Java的入门课程,上学期花了一半时间专门研究MATLAB,所以我确实有一点 …

c

0
推荐指数
1
解决办法
63
查看次数

标签 统计

c ×1

formatting ×1

matlab ×1

newline ×1