简体   繁体   English

八度选择一个文件?

[英]Octave select a file?

Does Octave have a good way to let the user select an input file? Octave 有让用户选择输入文件的好方法吗? I've seen code like this for Matlab, but doesn't work in Octave.我已经在 Matlab 中看到过这样的代码,但在 Octave 中不起作用。

A gui based method would be preferred, but some sort of command-line choice would work also.首选基于 gui 的方法,但也可以使用某种命令行选择。 It would be great if there were some way to do this that would work in both Matlab and Octave.如果有某种方法可以在 Matlab 和 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.我为 Matlab 找到了这个,但它在 Octave 中不起作用,即使您为 listdlg 函数安装Octave Forge Java 包也是如此。 In Octave, dir() gives you:在 Octave 中,dir() 为您提供:

  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.但我不知道如何将其转换为 listdlg 期望的字符串数组。

You have already the Octave Forge java package installed, so you can create instances of any java class and call any java method.您已经安装了Octave Forge java 包,因此您可以创建任何 java 类的实例并调用任何 java 方法。

For example to create a JFileChooser and call the JFileChooser.showOpenDialog(Component parent) method:例如创建一个JFileChooser并调用JFileChooser.showOpenDialog(Component parent)方法:

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.这是 Ubuntu 的修复程序 that worked also for my Debian Testing.这也适用于我的 Debian 测试。

EDIT编辑

@NoBugs In reply to your comment: @NoBugs 回复您的评论:

If you need to use listdlg you can do the following:如果您需要使用 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

This should be compatible with matlab, by I cannot test it right now.这应该与 matlab 兼容,因为我现在无法对其进行测试。

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

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. Octave 提供了uigetdiruigetfile函数,它们uigetfile您的期望。

I never came across an open-file-dialog in octave.我从来没有遇到过八度的打开文件对话框。
If you are looking for a gui based method maybe guioctave can help you.如果您正在寻找基于 gui 的方法,也许guioctave可以帮助您。 I never used it, because it appears only be available for windows machines.我从来没有用过它,因为它似乎只适用于 Windows 机器。

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.一个可能的解决方案是以八度写一个小脚本,这将允许用户解析目录并选择这样的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM