Octave 选择一个文件?

发布于 2024-12-05 07:40:03 字数 590 浏览 1 评论 0原文

Octave 有没有好的方法让用户选择输入文件?我在 Matlab 中见过这样的代码,但在 Octave 中不起作用。

基于 GUI 的方法将是首选,但某种命令行选择也可以。如果有某种方法可以在 Matlab 和 Octave 中工作,那就太好了。

我发现 this 用于 Matlab,但它在 Octave 中不起作用,即使您为 listdlg 函数安装 Octave Forge Java 包。在 Octave 中, dir() 为您提供:

  647x1 struct array containing the fields:

    name
    date
    bytes
    isdir
    datenum
    statinfo

但我不知道如何将其转换为 listdlg 期望的字符串数组。

Does Octave have a good way to let the user select an input file? I've seen code like this for Matlab, but doesn't work in Octave.

A gui based method would be preferred, but some sort of command-line choice would work also. It would be great if there were some way to do this that would work in both Matlab and Octave.

I found this for Matlab but it does not work in Octave, even when you install Octave Forge Java package for the listdlg function. In Octave, dir() gives you:

  647x1 struct array containing the fields:

    name
    date
    bytes
    isdir
    datenum
    statinfo

but I don't know how to convert this to an array of strings listdlg expects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单挑你×的.吻 2024-12-12 07:40:03

您已经安装了 Octave Forge java 包,因此您可以创建任何 java 类的实例并调用任何java方法。

例如,创建一个 JFileChooser 并调用 JFileChooser.showOpenDialog(Componentparent) 方法:

frame = javaObject("javax.swing.JFrame");
frame.setBounds(0,0,100,100);
frame.setVisible(true);
fc = javaObject ("javax.swing.JFileChooser")
returnVal = fc.showOpenDialog(frame);
file = fc.getSelectedFile();
file.getName()

顺便说一句。我在安装该软件包时遇到了一些麻烦。
这是 修复 Ubuntu 的问题。这也适用于我的 Debian 测试。

编辑

@NoBugs 回复您的评论:

如果您需要使用listdlg,您可以执行以下操作:

d = dir;
str = {d.name};
[sel,ok] = listdlg('PromptString','Select a file:',...
                'SelectionMode','single',...
                'ListString',str);
if ok == 1
    disp(str{sel(1)});
end

这应该与matlab兼容,但我现在无法测试它。

如果您想选择多个文件,请使用以下命令:

d = dir;
str = {d.name};
[sel,ok] = listdlg('PromptString','Select a file:',...
                'SelectionMode','multiple',...
                'ListString',str);
if ok == 1
   imax = length(sel);
   for i=1:1:imax
      disp(str{sel(i)});
   end
end

You have already the Octave Forge java package installed, so you can create instances of any java class and call any java method.

For example to create a JFileChooser and call the JFileChooser.showOpenDialog(Component parent) method:

frame = javaObject("javax.swing.JFrame");
frame.setBounds(0,0,100,100);
frame.setVisible(true);
fc = javaObject ("javax.swing.JFileChooser")
returnVal = fc.showOpenDialog(frame);
file = fc.getSelectedFile();
file.getName()

Btw. I had some troubles installing the package.
Here is a fix for Ubuntu. that worked also for my Debian Testing.

EDIT

@NoBugs In reply to your comment:

If you need to use listdlg you can do the following:

d = dir;
str = {d.name};
[sel,ok] = listdlg('PromptString','Select a file:',...
                'SelectionMode','single',...
                'ListString',str);
if ok == 1
    disp(str{sel(1)});
end

This should be compatible with matlab, by I cannot test it right now.

If you want to select multiple files use this:

d = dir;
str = {d.name};
[sel,ok] = listdlg('PromptString','Select a file:',...
                'SelectionMode','multiple',...
                'ListString',str);
if ok == 1
   imax = length(sel);
   for i=1:1:imax
      disp(str{sel(i)});
   end
end
万劫不复 2024-12-12 07:40:03

我从未遇到过八度音程中的打开文件对话框。
如果您正在寻找基于 GUI 的方法,也许 guioctave 可以帮助您。我从未使用过它,因为它似乎只适用于 Windows 机器。

一个可能的解决方案是在八度音阶中编写一个小脚本,这将允许用户解析目录并选择这样的文件。

I never came across an open-file-dialog in octave.
If you are looking for a gui based method maybe guioctave can help you. I never used it, because it appears only be available for windows machines.

A possible solution would be to write a little script in octave, that would allow the user to parse through the directories and select a file like that.

凉月流沐 2024-12-12 07:40:03

我想我会为这个老问题提供更新的答案,因为它出现在其他问题的“相关问题”字段中。

Octave 提供了 uigetdir 和 uigetfile 函数,可以满足您的期望。

Thought I'd provide an updated answer to this old question, since it is appearing in the 'related questions' field for other questions.

Octave provides the uigetdir and uigetfile functions, which do what you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文