Editing file name when using export-view in Netlogo

拟墨画扇 提交于 2020-03-25 05:52:08

问题


I want to use export-view to save an image of my model. I have made a button in the interface to export image (JPG, PNG, or PDF) from the Netlogo.

this is my current code in the export-view button

file-open user-new-file
export-view (word "view1.jpg")
set view-number view-number + 1

Currently, file-open command helps in showing a pop-up input window before saving. There is a runtime error of "FILE-OPEN expected input to be a string but got the TRUE/FALSE false instead". I can still save the file but this pop's up from time to time

At first try the I can save the files successively. Now, it only saves one file named view1 everytime. Is there something wrong with new code?


回答1:


Have a look at the user-new-file primitive in the NetLogo dictionary. That allows you to get the user input and then you can use the word primitive to save in the same way that you are doing it now.

The short version of complete code is:

to testme1
  export-view user-new-file
end

To get a full idea of what's going on, here's a longer version:

to testme2
  let fn user-new-file
  print fn
  set fn word fn ".png"
  print fn
  if file-exists? fn [file-delete fn]
  file-open fn
  export-view fn
  file-close
end

So what actually happens is the user-new-file returns a string for whatever the user enters. You can use that string directly with the export-view, or you can manipulate it a bit and then use it. I'm not completely clear why you are getting that particular error, but the code you have is creating/opening a file with a different name than the name you try and export to.

For example, in my testme2 code, I appended the 'png' extension on the assumption that the user did not type this. In a real application, you could look at the last 3 characters and add the extension only if required for example. My longer code also deletes any existing file of that name - I don't think this is required for png views as I think NetLogo simply overwrites, but exporting to csv would add lines at the end.




回答2:


JenB's answer may be exactly what you are looking for. But to cover another possibility suggested by your original filename, you may be looking to number multiple export files, perhaps exported every few ticks or when something interesting occurs, or as you have it, whenever the export-view button is pressed. In that case, if you define a global variable such as view-number, you could then use the code

export-view (word "view" view-number ".jpg")
set view-number view-number + 1

This will give you successive files "view0.jpg", "view1.jpg", "view2.jpg" ... view-number is initialized at 0, but you could start with some other number if you wish.

Charles



来源:https://stackoverflow.com/questions/60796314/editing-file-name-when-using-export-view-in-netlogo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!